- Home /
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.
Answer by Eno-Khaon · Sep 09, 2015 at 07:45 PM
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.
The code almost worked actually! It's just that the strafe buttons (A and D keys) aren't working at all.
Also, why is there a Vector2
type with a Vector3
value?
EDIT: Never$$anonymous$$d! I've solved it now! I just replaced:
Vector3 newRight = Vector3.Cross(Vector3.up, newForward).normalized * directionInput.y;
With:
Vector3 newRight = Vector3.Cross(Vector3.up, camTransform.forward).normalized * directionInput.y;
So everything is fine now!
Whoops! You got me there! Nice catch.
I was jumping between a few ideas and forgot to change the value from a "new Vector3()" to a "new Vector2()" in the post.
Also, I'm a little confused at how there could be a difference between the means used to calculate a "new right" direction, since the Cross Product using either "forward" direction should be the same in 99% of circumstances, but as long as you have it working, I'm not about to complain.
Your answer
Follow this Question
Related Questions
how do i make first person character rotate left and right along with camera? 0 Answers
Problem with jumping in fps game 1 Answer
view bounds perspective camera 1 Answer
I cannot see button on camera preview 1 Answer
Scope Zoom Sensitivity 0 Answers