- Home /
Rotate character acording to buttons pressed
I wanna make so my character changes rotation based on keys presssed. Dont worry about the movement. Look at this script doesnt work but explains exactly what i need, hard angles and all:
public class PlayerController : MonoBehaviour
{
public float playerSpeed;
Vector3 playeraxis;
public CharacterController cctrl;
public bool groundedPlayer;
public Vector3 playerVelocity;
public float gravityValue = -9;
void Update()
{
playeraxis = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
cctrl.SimpleMove(playeraxis * playerSpeed);//Move?
if (Input.GetKey(KeyCode.W))
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = Quaternion.Euler(0, 180, 0);
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.Euler(0, 90, 0);
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.Euler(0, -90, 0);
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.Euler(0, -45, 0);
}
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.Euler(0, 45, 0);
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.Euler(0, 225, 0);
}
if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.Euler(0, 135, 0);
}
}
}
Also, probably unecessary to say, but i want it to be possible to adapt to controller input. Thanks for reading.
Comment