Make character move according to camera position.

Hi. I’m new to unity (really new) and i’m having a problem trying to make the character move according to the camera view.

I used the transform.Translate to make the character move according to the global coordinates. but when I move the camera, i have that problem when I move the camera in which the forward direction is the backwards for the camera view. I wanted that it changed according to what I’m seeing on the camera. I searched everywhere, but the solutions I found either didn’t work, or I didn’t know how to adapt into the code I’m using (beucase of the inexperience, maybe)

The code I’m using on the character is:

public class PlayerController : MonoBehaviour {

 private Rigidbody rb;
 public float speed = 10.0F;
 public float jump = 3.0F;
 public float run = 20.0F;
 Animator anim;
 void Start ()
 {
     rb = GetComponent<Rigidbody> ();
     anim = GetComponent<Animator> ();
 }
         
 void Update()
 {
     float moveHorizontal = Input.GetAxis ("Horizontal") ;
     float moveVertical = Input.GetAxis ("Vertical");
                         
     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
     if (movement != Vector3.zero) 
     {
         transform.Translate (movement * speed * Time.deltaTime, Space.World);
         anim.SetBool ("walk", true);
         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(movement), 0.2f);
     }
     else
     {
         anim.SetBool ("walk", false);
     }
     if (movement != Vector3.zero && Input.GetButton("Fire2"))
     {            
         transform.Translate (movement * run * Time.deltaTime, Space.World);
         anim.SetBool ("fastRun", true);
         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(movement), 0.2f);
     }
     else
         anim.SetBool ("fastRun", false);
     if (Input.GetButtonDown("Fire1")) 
     {
         if (anim.GetBool ("walk") == false)
             anim.SetTrigger ("Jump");
         else 
         {
             rb.AddForce (0.0f, jump, 0.0f, ForceMode.Impulse);
             anim.SetTrigger ("Jump");
         }
     }
 
 }

}

To move the camera i’m using:

public class CameraControl : MonoBehaviour {

 public GameObject Player;
 public float rotationSpeed = 4.0f;
 private Vector3 offset;
 void Start ()
 {
     offset = transform.position - Player.transform.position;
 }
 void LateUpdate ()
 {
     offset = Quaternion.AngleAxis (Input.GetAxis ("Mouse X") * rotationSpeed, Vector3.up) * offset;
     offset = Quaternion.AngleAxis (Input.GetAxis ("Mouse Y") * rotationSpeed, Vector3.right) * offset;
     transform.position = Player.transform.position + offset;
     transform.LookAt (Player.transform.position);
         
 }

}

Can anyone help me? (I’m also having a problem trying to set a limit to the quaternion.angleaxis, so the camera doesn’t go through the ground, but one problem at a time).

In general you’ll parent the camera to the player’s transform. If it’s a first person game, you’ll parent the camera somewhere in or near the character’s head. To solve the issue with rotation going into the ground, look at Mathf.Clamp.