2017-05-09 1 views
0

플레이어가 세계를 움직일 때 플레이어가 스스로 주위를 돌릴 수있는 2 차원 탑 다운 게임을 만들고 있습니다.카메라가 회전하고 움직일 때의 지저분한 움직임 - 2D 탑 다운 유니티

플레이어가 카메라를 잘 움직이는 전 세계를 돌아 다닐 때 모든 것이 잘 작동합니다. 그가 여전히 서 있으면 카메라 회전도 잘 작동합니다. 그러나 두 가지 작업을 모두 시작하자마자 카메라에 지터가 생겨서 다른 모든 객체가 플레이어와 지터가됩니다.

이제는이 문제를 해결하기 위해 rigidbody2d.AddRelativeForce (물리학)를 사용하여 플레이어를 이동시키고 그의 움직임을 FixedUpdate에서 확인한다는 사실을 알게되었습니다. 내 카메라 회전 및 LateUpdate, FixedUpdate에 다음 스크립트를 이동 시도

https://forum.unity3d.com/threads/camera-jitter-problem.115224/http://answers.unity3d.com/questions/381317/camera-rotation-jitterness-lookat.html

, 당신이 그것을 이름을 업데이트합니다. 아무것도 작동하는 것 같습니다. 나는 카메라의 움직임과 이것을 일으키는 회전 사이에 어떤 종류의 지연이 있다고 확신합니다. 누구든지 의견이 있으면 궁금합니다.

Vsync를 사용하지 않도록 설정했는데 완전히 제거하지 못했습니다. 강체를 보간 및 외삽하려고 시도했지만 차이가 있지만 완전히 제거하지는 못했습니다. 아이러니하게도 나는 그것을 아무에게도 설정하지 않으면 최상으로 작동합니다.

스크립트 :

문자를 따라이 스크립트는 플레이어에 적용 아이

public class FollowPlayer : MonoBehaviour { 

    public Transform lookAt; 
    public Spawner spawner; 
    private Transform trans; 
    public float cameraRot = 3; 

    private bool smooth = false; 
    private float smoothSpeed = 30f; 
    private Vector3 offset = new Vector3(0, 0, -6.5f); 

    //test 
    public bool changeUpdate; 

    private void Start() 
    { 
     trans = GetComponent<Transform>(); 
    } 

    private void FixedUpdate() 
    { 
     CameraRotation(); 
    } 

    private void LateUpdate() 
    { 
     following(); 
    } 

    public void following() 
    { 

     Vector3 desiredPosition = lookAt.transform.position + offset; 

     if (smooth) 
     { 
      transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); 
     } 
     else 
     { 
      transform.position = desiredPosition; 
     } 
    } 

    void CameraRotation() 
    { 
     if (Input.GetKey("q")) 
     { 
      transform.Rotate(0, 0, cameraRot); 
     } 

     if (Input.GetKey("e")) 
     { 
      transform.Rotate(0, 0, -cameraRot); 
     } 
    } 

    public void SetTarget(string e) 
    { 
     lookAt = GameObject.Find(e).GetComponent<Transform>(); 
    } 

} 문자 컨트롤러, 스크립트로 카메라를 가진 게임 오브젝트에 적용,라고 FixedUpdate에서

 private void HandleMovement() 
    { 

     if (Input.GetKey("w")) 
     { 
      rigid.AddRelativeForce(Vector2.up * speed); 
     } 

     if (Input.GetKey("s")) 
     { 
      rigid.AddRelativeForce(Vector2.down * speed); 
     } 

     if (Input.GetKey("a")) 
     { 
      if (facingRight) 
      { 
       Flip(); 
      } 
      rigid.AddRelativeForce(Vector2.left * speed); 
     } 

     if (Input.GetKey("d")) 
     { 
      if (!facingRight) 
      { 
       Flip(); 
      } 
      rigid.AddRelativeForce(new Vector2(1,0) * speed); 
     } 
    } 

답변

0

드디어이 문제 해결 :

먼저 내 cameraRotation에을 (만들어)를하고()에 따라 1 함수로 변환하고 더 명확하게 이름을 지정했습니다.

public void UpdateCameraPosition() 
{ 
    if (Input.GetKey("q")) 
    { 
     transform.Rotate(0, 0, cameraRot); 
    } 
    else if (Input.GetKey("e")) 
    { 
     transform.Rotate(0, 0, -cameraRot); 
    } 

    Vector3 desiredPosition = lookAt.transform.position + offset; 

    transform.position = desiredPosition; 
} 

그런 다음 이동을 처리 한 직후 문자 컨트롤러에서이 함수를 호출했습니다. fixedUpdate의 두 호출 (handleMovement & cameraPosition).

void FixedUpdate() 
{ 
    HandleMovement();       
    cameraController.UpdateCameraPosition(); 
} 

지터가 사라졌습니다.

내가 읽었던 이전 게시물에서 두 통화 사이에 약간의 지연이 있었기 때문에 암시적인 것처럼 보입니다. 그러나 나는 그들을 충분히 가깝게 적절하게 설정할 수 없었습니다.

희망이 있으면 도움이됩니다.

1

코 루틴을 사용해보십시오. 내 스크립트 중 일부를 수정하여 코드에 익숙하게하고 시도해 보았지만 전혀 지저분하지 않았습니다. 나는 그것이 당신을 도울 것이기를 바랍니다.

카메라 클래스 :

public class CameraController : MonoBehaviour { 

    [SerializeField] 
    Transform CameraRotator, Player; 

    [SerializeField] 
    float rotationSpeed = 10f; 

    float rotation; 

    bool rotating = true; 

    void Start() 
    { 
     StartCoroutine(RotatingCamera()); 
    } 

    IEnumerator RotatingCamera() 
    { 
     while (rotating) 
     { 
      rotation = Input.GetAxis("HorizontalRotation"); 
      CameraRotator.Rotate(Vector3.up * Time.deltaTime * rotation * rotationSpeed); 
      CameraRotator.position = Player.position; 
      yield return new WaitForFixedUpdate(); 
     } 
    } 

    private void OnDestroy() 
    { 
     StopAllCoroutines(); 
    } 
} 

플레이어 클래스 :

public class PlayerMovement : MonoBehaviour { 

[SerializeField] 
float movementSpeed = 500f; 

Vector3 movementVector; 
Vector3 forward; 
Vector3 right; 

[SerializeField] 
Transform CameraRotator; 

Rigidbody playerRigidbody; 

float inputHorizontal, inputVertical; 

void Awake() 
{ 
    playerRigidbody = GetComponent<Rigidbody>(); 

    StartCoroutine(Moving()); 
} 

IEnumerator Moving() 
{ 
    while (true) 
    { 
     inputHorizontal = Input.GetAxis("Horizontal"); 
     inputVertical = Input.GetAxis("Vertical"); 

     forward = CameraRotator.TransformDirection(Vector3.forward); 
     forward.y = 0; 
     forward = forward.normalized; 

     Vector3 right = new Vector3(forward.z, 0, -forward.x); 

     movementVector = inputHorizontal * right + inputVertical * forward; 

     movementVector = movementVector.normalized * movementSpeed * Time.deltaTime; 

     playerRigidbody.AddForce(movementVector); 
     yield return new WaitForFixedUpdate(); 
    } 
} 

}

+0

안녕하세요, 일부 입력 해 주셔서 감사합니다. 나는 당신의 스크립트를 당신의 실행에 넣으려고 노력했다. 그리고 그들에게서 무엇이라도 얻지 않는다. 나는 2D를 사용하고 있기 때문에 아무 것도 매달지 않는다. 나는 카메라 스크립트로 플레이어 스크립트와 새로운 카메라로 새로운 플레이어를 만들었습니다. – Svp

+0

https://www.youtube.com/watch?v=sCqQYILenzw 여기에 문제가 발생한 영상이 있습니다. – Svp

관련 문제