- Home /
How to move a tumbling player relative to the camera?
I have been googling this a lot and have found lots of information. However that information doesn't seem to work in my case. I have a tumbling box that is moved around the scene currently with 'rigidbody.velocity.z=speed' etc. However this is an issue when the player moves the player as all the controls become inverted as the controls are working from world directions. How can I make it so that the when I press the 'D' key, the player will always appear to go right from the cameras perspective.
Here is the code for the Camera follow: var target : Transform; var damp : float = 5; var distance : float = 20;
function Update(){
damp = Mathf.Clamp(damp, 0.01, 10);
var pCam = transform.position;
var pTarget = target.position;
var diff: Vector3 = pTarget - pCam;
var dist = diff.magnitude;
if (Mathf.Abs(diff.y) < 0.7*distance){
diff.y = 0;
}
if (dist>distance){
diff *= 1-distance/dist;
transform.position = pCam + diff * Time.deltaTime/damp;
}
transform.LookAt(pTarget);
}
And the script for the player to move is here. As you can see I have tried using 'cam.TransformDirection(Vector3.right);' and 'Camera.main.transform.transform.back*speed;' Neither result in any movement. I've remove any irrelevant parts to make it more clear:
#pragma strict
var speed : float = 500;
var cam : Transform;
var camRelativeRight : Vector3 = cam.TransformDirection(Vector3.right);
var camRelativeLeft : Vector3 = cam.TransformDirection(Vector3.left);
var camRelativeForward : Vector3 = cam.TransformDirection(Vector3.forward);
var camRelativeBack : Vector3 = cam.TransformDirection(Vector3.back);
function Update () {
if(Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.LeftShift)) {
camRelativeForward*speed;
}
if(Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.LeftShift)) {
Camera.main.transform.back*speed;
}
if(Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.LeftShift)) {
Camera.main.transform.right*speed;
}
if(Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.LeftShift)) {
Camera.main.transform.left*speed;
}
}
The player is not a parent of the camera. This would result in the camera tumbling around as the player game object tumbles. I wasn't sure if this could be the issue.
Thank you in advance for any help :).
Answer by Loius · Jun 21, 2014 at 08:10 PM
You need to transform your input vector from world space into camera space.
The function for this is either Transform.TransformDirection or Transform.InverseTransformDirection (I always forget).
Vector3 relativeInput = Camera.main.transform.TransformDirection(worldInput);
You have to do this in Update, since the camera is moving all the time. The way you have it is only checking the camera's orientation one time at the start.
Answer by siaran · May 26, 2014 at 02:28 PM
I'm not seeing any code that updates your player's position. In your player update script try changing camRelativeForward * speed;
to transfrom.position += camRelativeForward*speed * Time.deltaTime;
(and also for the other directions)
I'm having issues with the camRelativeForward variable. It throws this error: NullReferenceException: Object reference not set to an instance of an object playerControl..ctor () (at Assets/playerControl.js:5) (Line 5 in the full script is the camRelativeRight variable. The first of the relative direction variables) I understand that this could have meant that I hadn't set the target object. However the target is correctly set to the player. If it wasn't the camera wouldn't be able to follow the player through the Transform.LookAt thing. The camera works perfectly it's just that I can't get controls relative to the camera working. Thanks for your help Siaran. I've tried it and it doesn't work. But I think it is because the camRelativeForward variable is broken. Not because your answer is incorrect.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145925im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to relative movement system based on camera direction 1 Answer
C# AddForce Relative To Camera Left And Right Movement 1 Answer
How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers
Camera Relative Movement 2 Answers
Camera relative rigidbody third person player movement 1 Answer