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 :).