2016-06-05 4 views
0

Ive는 내가 머리를 감싸는 문제를 일으켰습니다.카메라 상대 움직임

Im은 Unity를 통해 하향식 투시 3D 게임을 만듭니다. 움직이는 키를 누르면 캐릭터가 회전하고 움직일 수있는 컨트롤러가 있습니다. 예를 들어 "D"를 누르면 플레이어 오브젝트가 오른쪽으로 향한 다음 rigidBody가 그 방향으로 움직입니다. 그러나 카메라를 회전 시키면 플레이어가 카메라 방향에 대해 오른쪽으로 움직이지 않습니다.

적십자 운동 : 그는 오일러 각도와 내가 알아 내려고하고 반환 할 수 설정된 카메라를 가지고,

//Player Movement 
    void HandleMove() 
    { 
     if (canMove == true) 
     { 
      //Detect input movement 
      var moveHorizontal = Input.GetAxis("Horizontal"); 
      var moveVertical = Input.GetAxis("Vertical"); 
      IsMoving = moveHorizontal != 0 || moveVertical != 0; 

      Vector3 relative = transform.rotation.eulerAngles; 
      relative.y = pCamera.playerRelativeAngle; 

      //Rotate the character 
      var movement = new Vector3(moveHorizontal, 0.0f, moveVertical); 
      var rot = movement * (speed/10); 

      if (attackTimer <= 0 && movement != Vector3.zero) 
      { 
       Vector3 temp = transform.rotation.eulerAngles; 
       var newRotation = Quaternion.LookRotation(rot); 
       transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, 10F); 
      } 

      //Move the character 
      if (IsMoving && offset.y != 0f) 
      { 
       movement.y = offset.normalized.y * movement.magnitude; 
      } 

      var characterMovement = transform.position + movement; 
      if (attackTimer <= 0 || !IsGrounded) 
      { 
       rbody.MovePosition(characterMovement); 
      } 
    } 

나는 무엇을에 관해서는 약간의 아이디어를 가지고 있지만 완전히 파악 할 수 상대 운동을 위해 이것을 사용하는 방법이지만, 내 운동 contoller에서 그것을 적용 할 위치 나 방법을 모르겠습니다.

카메라 : 어떤 조언을 크게 감사

//Find camera angle 
public static float playerRelativeAngle; 

//On awake, do this 
void Awake() 
{ 
    //Get the cameras angle so we can move the player relative to it??? 
    playerRelativeAngle = this.transform.localEulerAngles.y; 
} 

//Update every frame 
void Update() 
{ 
    playerRelativeAngle = this.transform.localEulerAngles.y; 

    //If the right key is pressed then the caerma rotates to the right 
    if (Input.GetKey("right")) 
    { 
     this.transform.RotateAround(target.transform.position, Vector3.down, cameraSpeed * Time.deltaTime); 
    } 
    if (Input.GetKey("left")) 
    { 
     this.transform.RotateAround(target.transform.position, Vector3.down, -cameraSpeed * Time.deltaTime); 
    } 
} 

.

답변

0

나는 내 문제에 대한 해결책을 찾을 수있었습니다.

운동이다

   //Rotate the character relative to the camera direction 
      if (attackTimer <= 0 && moveDirection != Vector3.zero) 
      { 
       var targetDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); 
       targetDirection = Camera.main.transform.TransformDirection(targetDirection); 
       targetDirection.y = 0.0f; 
       Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up); 
       float rotationSpeed = 100.0f; 
       Quaternion newRotation = Quaternion.Lerp(rbody.rotation, targetRotation, rotationSpeed * Time.deltaTime); 

       //Apply the rotation 
       rbody.MoveRotation(newRotation); 
      } 

      //Move the character 
      if (IsMoving == true) 
      { 

       if (Input.GetAxis("Vertical") != 0) //Vertical movement (Up/Down) 
       { 
        if (attackTimer <= 0 || !IsGrounded) 
        { 
         rbody.AddForce(transform.forward * speed); 
        } 
       } 

선수 오브젝트가 현재 카메라에 대해 회전하고 플레이어에 이동 키가 눌러 전진 방향의 힘을인가한다.

완벽한 해결책은 아니지만 앞으로 누군가에게 도움이 될 수 있기를 바랍니다.