- Home /
Player's movements doesn't match orbital camera
Hey, I made an orbital camera that rotates around my player, but the player doesn't move relative to the camera. I've made it so that the player moves in the forward facing direction of the camera when you press W but I can't figure out how to make the player move right, left, or down from the camera facing direction. Any ideas on how I can fix this?
Heres my script:
public Animator animator;
public float speed = 5.0f;
public float horizontalInput;
public float forwardInput;
// Update is called once per frame
void LateUpdate()
{
if (Input.GetKey(KeyCode.W))
{
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
this.transform.Translate(Vector3.forward * Time.deltaTime * speed);
this.gameObject.transform.rotation = Quaternion.Euler(0f, Camera.main.transform.eulerAngles.y, 0f);
animator.SetBool("Move", true);
}
if (Input.GetKey(KeyCode.S))
{
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * Time.deltaTime * speed);
gameObject.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
animator.SetBool("Move", true);
}
if (Input.GetKey(KeyCode.A))
{
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * Time.deltaTime * speed);
gameObject.transform.rotation = Quaternion.Euler(0f, 90f, 0f);
animator.SetBool("Move", true);
}
if (Input.GetKey(KeyCode.D))
{
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * Time.deltaTime * speed);
gameObject.transform.rotation = Quaternion.Euler(0f, -90f, 0f);
animator.SetBool("Move", true);
}
}
Answer by Magso · Feb 17, 2020 at 10:09 PM
Is the player a child of the camera? I'm wondering but you're setting the rotation to exact amounts transform.rotation = Quaternion.Euler(0f, 90f, 0f);
if this isn't the case then you had it right to begin with; you can simply add the rotation on.
transform.rotation = Quaternion.Euler(0f, Camera.main.transform.eulerAngles.y + 90f, 0f);
Also this.gameObject.transform
refers to this script component to the gameObject to the transform. Like how you use Translate()
, you can start straight from transform
.