# Rotating player relative to the camera (Unity C#)

So I’ve been thinking about this problem for a long time now. As the beginner that I am, I decided to make a game of my own in Unity, with simple controls like moving a cube, and rotating the camera around the player. Now my only problem is moving the character relative to the camera. I am using the MouseOrbitImproved script for the camera rotation. The player controls are as follows below:

``````public float moveSpeed;
public static float jumpHeight = 5f;
public static Vector3 input;
public static Vector3 movement;
public static Rigidbody rigidBody;

public static bool isFalling = false;

void Start () {
rigidBody = GetComponent<Rigidbody>();
}

void Update () {
rigidBody.velocity = new Vector3 (Input.GetAxis ("Horizontal") * moveSpeed, rigidBody.velocity.y, Input.GetAxis ("Vertical") * moveSpeed);
if (Input.GetKeyDown (KeyCode.Space) && isFalling == false)
rigidBody.velocity = new Vector3 (0, jumpHeight, 0);
isFalling = true;
}

void OnCollisionStay()
{
isFalling = false;
}
``````

What I know is that I need to make the cube rotate in the y axis relative to the camera. I’ve already tried quaternions and other solutions like finding other scripts deep down in the Unity packages but I still don’t have a definite answer.

In general, this can be handled by keeping a reference to your camera’s transform available. Then, you can use that reference to modify your controls.

For example, for direct control over the character, you can do something like:

``````public Transform camTransform;

// ...

Vector2 directionInput = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, Input.GetAxis("Vertical") * moveSpeed);
// Eliminate camera pitch and/or roll influence by reconstructing the forward and right vectors to align to world space
Vector3 newForward = Vector3.Cross(camTransform.right, Vector3.up).normalized * directionInput.x;
Vector3 newRight = Vector3.Cross(Vector3.up, newForward).normalized * directionInput.y;

Vector3 newDirectionInput = newForward + newRight;
``````

On a related note, I would recommend modifying your control schemes to be based around AddForce() rather than directly modifying velocity (especially for jumping input), but however you want to approach that is up to you.

Edit: It’s also worth noting that rotations can be derived from this on the same basis. By deriving flattened rotations based on the camera’s facing, your character can rotate to face in those directions as well.