- Home /
This question was
closed Aug 11, 2014 at 05:09 PM by
ado112 for the following reason:
The question is answered, right answer was accepted
Movement direction based on camera's direction but using only one axis for movement.
my script:
var movementSpeed : float = 2.0;
function Update () {
var cameraDirection = Camera.main.transform.TransformDirection(0,0,1);
if(Input.GetKey(KeyCode.W)){
rigidbody.MovePosition(rigidbody.position+cameraDirection*movementSpeed*Time.deltaTime);
}
}
and camera is using script from here,when i look at object from bird's perspective,i see that object is slower and starts to go down(trying to move down),how can i make it move only foward?
Comment
Best Answer
Answer by robertbu · Aug 11, 2014 at 05:02 PM
Try this: It works by taking the 'y' component out of the vector and normalizing the result:
var movementSpeed : float = 2.0;
function Update () {
if(Input.GetKey(KeyCode.W)){
var cameraDirection = Camera.main.transform.forward;
cameraDirection.y = 0.0;
cameraDirection.Normalize();
rigidbody.MovePosition(rigidbody.position+cameraDirection*movementSpeed*Time.deltaTime);
}
}