Help! when I turn 180 degrees in my game the controls don’t follow, so the controls would be flipped, “W” would be backward and “S” would be forward, etc. Any advice?
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float runSpeed = 5f;
public float speedH = 2f;
public float speedV = 2f;
private float yaw = 0.0f;
private float pitch = 0.0f;
// Start is called before the first frame update
void Start()
{
runSpeed = 5f;
}
// Update is called once per frame
void Update()
{
transform.Translate(runSpeed * Input.GetAxis(“Horizontal”) * Time.deltaTime, 0f, runSpeed * Input.GetAxis(“Vertical”) * Time.deltaTime, 0f);
yaw += speedH * Input.GetAxis(“Mouse X”);
pitch -= speedV * Input.GetAxis(“Mouse Y”);
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
did u try
transform.Rotate(axis, rotationDegree,Space.Self);
for local rotation
i remember when trying to rotate i noticed that euler angles were not what i expected
because rotation is done using quaternions internally and for display purpose it is available as euler.
Could you rewrite the line of code that i need to change to make it work? or show me an example. I dont know where to put the code you told me to write.
its moving fast becos the prev yaw & pitch values are being used to rotate again & again
if u don’t want that
then call rotate when u need it
the risk in using transform.eulerangles
It doesn’t matter. the controls just don’t follow the rotation, so speaking globally if you turned 90 on the Y the controls would be “W” for moving left and “S” would be moving right “A” would be backward and “D” would be forward.
Put your character (the thing you want to control) in an empty game object. Apply the movement on that but apply the rotation on the original. So you will have a GO which does not rotate and always moves in relation to the WASD keys (or what you set), but the visual elements and the inner character will rotate according to the desired rotation.