- Home /
 
The question is answered, right answer was accepted
How to move and rotate character according to camera?
I stuck on movement system according to camera. When i move from back of character, everything is ok. But when i look from top of character (top-down), character don't move (trying to go down). Anyone help please. And how can i add character look to input (with lerp) in this example.
 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.CrossPlatformInput;
 
 public class Player : MonoBehaviour
 {
     public TPCamera tpCamera;
     Vector2 input;
     Rigidbody rigid;
     public float moveSpeed;
     float speed, direction;
     Quaternion _rotation;
 
     void Start ()
     {
         rigid = GetComponent<Rigidbody> ();
     }
 
     void LateUpdate ()
     {
         CameraInput ();
     }
 
     void CameraInput ()
     {
         #if MOBILE_INPUT
         tpCamera.mouseX += CrossPlatformInputManager.GetAxis ("Mouse X") * tpCamera.X_MouseSensitivity;
         tpCamera.mouseY -= CrossPlatformInputManager.GetAxis ("Mouse Y") * tpCamera.Y_MouseSensitivity;
         #else
         tpCamera.mouseX += Input.GetAxis ("Mouse X") * tpCamera.X_MouseSensitivity;
         tpCamera.mouseY -= Input.GetAxis ("Mouse Y") * tpCamera.Y_MouseSensitivity;
         #endif
         tpCamera.mouseY = Helper.ClampAngle (tpCamera.mouseY, tpCamera.Y_MinLimit, tpCamera.Y_MaxLimit);
         tpCamera.mouseX = Helper.ClampAngle (tpCamera.mouseX, tpCamera.X_MinLimit, tpCamera.X_MaxLimit);
     }
 
     void FixedUpdate ()
     {
         ControllerInput ();
         ControlLocomotion ();
     }
 
     void ControllerInput ()
     {
         #if MOBILE_INPUT
         input = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), CrossPlatformInputManager.GetAxis("Vertical"));    
         #else
         input = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));
         #endif
         input.Normalize ();
     }
 
     void ControlLocomotion ()
     {
         //Not sure is this a good solution to rotate ???
     if (input != Vector2.zero)
     {
         _rotation = Quaternion.LookRotation(targetDirection, Vector3.up);
         Vector3 lookDirection = targetDirection.normalized;
         _rotation = Quaternion.LookRotation(lookDirection);
         rigid.rotation = Quaternion.Slerp(transform.rotation, _rotation, rotationSpeed);
     }
     //Move Character with rigidbody according to camera ignoring y
     }
 
     Vector3 targetDirection {
         get {
             Vector3 cameraForward = tpCamera.transform.TransformDirection (Vector3.forward);
             cameraForward.y = 0;    //set to 0 because of camera rotation on the X axis
             
             //get the right-facing direction of the camera
             Vector3 cameraRight = tpCamera.transform.TransformDirection (Vector3.right);
             
             // determine the direction the player will face based on input and the camera's right and forward directions
             Vector3 refDir = input.x * cameraRight + input.y * cameraForward;
             return refDir;
         }
     }
 }
 
              Dont you need to rotate/move the character based on the camera up direction ins$$anonymous$$d of the forward(talking about top down mode)?
Character must always move and rotate. Camera is controlled by mouse. When I look from top, character doesn't move. (i am not good at english, sorry i dont get your question) Edit: i removed rigid.velocity from script.
So, when your camera is looking from top, try using the camera up direction ins$$anonymous$$d of forward. Does this test work?
Follow this Question
Related Questions
Character and Camera motion smooth in Editor, but jitters in Build 1 Answer
How to prevent camera jitter when moving my Rigidbody first person controller 4 Answers
Moving character towards direction fast 2 Answers
Running wind effect 1 Answer
How to fix AI moving with my Input instead of self navigation 1 Answer