how to make forward relative to the view of the camera
hello, I am currently making my first game in unity and i seam to be going good so far. I have made a movement system to roll the player(the player controls a ball) that works like this.
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
I have the camera floating a bit behind the player using a camera holder inside the player. the reason I use a camera holder is so that when the player rolls the ball the camera will not rotate with the ball. The camera controller script just keeps the camera at the right angle and distance from the ball and looks like this.
public GameObject CameraHolder;
private Vector3 offset;
void Start () {
offset = CameraHolder.transform.position - transform.position;
}
void LateUpdate () {
transform.position = CameraHolder.transform.position + offset;
float desiredAngleX = CameraHolder.transform.eulerAngles.y;
float desiredAngleY = CameraHolder.transform.eulerAngles.x;
Quaternion rotation = Quaternion.Euler(desiredAngleY, desiredAngleX, 0);
transform.position = CameraHolder.transform.position - (rotation * offset);
transform.LookAt(CameraHolder.transform);
}
}
Everything was working fine up to this point until i wanted to make the camera rotate so you could look around. i use this script to make the camera holder rotate.
public float RotateX = 0.0f;
public float RotateY = 0.0f;
public float RotateSpeed = 1.0f;
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void Update () {
transform.Rotate (RotateY, RotateX, 0.0f);
transform.position = player.transform.position + offset;
RotateX = Input.GetAxis("Mouse X") * RotateSpeed;
RotateY = Input.GetAxis("Mouse Y") * -RotateSpeed;
transform.Rotate(RotateY, RotateX, 0.0f);
transform.position = player.transform.position + offset;
RotateX = Input.GetAxis("Joystick X") * RotateSpeed;
RotateY = Input.GetAxis("Joystick Y") * RotateSpeed;
}
now the camera will rotate around the ball like i want but the problem is with the movement. when i start the game and do not move the camera movement works like it should. up goes forwards down goes backwards ect. but when i rotate the camera the movement doesn't change. if i were to start the game and rotate the camera 180 degrees than up would go backwards what can i change to get the movement to be relative to the camera so that up will always go away from the camera and left will always go to the left of the camera ect.
sorry if i am not doing this post well this is my first tome posting.
sorry i figured it out after doing more research. just add movement = Camera.main.transform.TransformDirection(movement); after
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);