- Home /
Moving player in direciton camera is facing
Hello, i am making i space game like "Freelancer" or equivalent and i am working on the movement. My plan is that when the left and right arrow keys / A & D are pressed the character will spin on the horizontal axis, which it does, but when i move the mouse down so its facing the floor and then press the up / w i just goes like i am looking forward, i am wanting to go up if i am looking up but down if i am looking down. i have set the first person control camera script and took off the others so i just have camera movement and i have disabled gravity. i have this code;
var speed : float = 3.0;
var rotateSpeed : float = 3.0;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis
transform.Rotate(Vector3.right, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetKeyDown ("Vertical");
controller.Move(forward * curSpeed);
}
@script RequireComponent(CharacterController)
sorry if my question is confusing but basically which ever direction the camera is facing i want when i press movement key it go in that direction.
thanks in advance
Answer by nasapc123 · Sep 30, 2011 at 02:55 PM
you will want to get the camera transform.forward and use that to base the characters movement on. make a variable that stores the camera object:
var cameraObject : GameObject;
change this:
controller.Move(forward * curSpeed); to:
controller.Move(cameraObject.transform.forward * curSpeed);
and assign the camera you want to use to the variable in the inspector :) hope this helps you :)
Your answer
Follow this Question
Related Questions
How to synchronize both movement and rotation? 1 Answer
How do i change the player camera to a free view? 0 Answers
Camera Movement and angles 2 Answers
Help needed moving this gameobject 0 Answers