Hi, kind people of unity forums. I am having trouble with my script as to how I can rotate the player while moving so it would look like the player is actually walking in different directions rather than “strafing” I need my player to rotate so he is always facing the direction he is going to with respect to the button pressed. Much like how it would behave with a joystick. Can anyone help me visualize the code? Here’s my rather simple script for player movement (with the help from one of the forum members)
public float speed = 5; // set speed
public float distancetoGround;
void Start()
{
distancetoGround = collider.bounds.extents.y;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Vector3.forward * speed * time.deltatime
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Vector3.back * speed * time.deltatime
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
]Vector3.left * speed * time.deltatime
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Vector3.Right * speed * time.deltatime
}
if (Input.GetKeyDown(KeyCode.Space)) && isGrounded())
{
// do jump // assuming you want to check if you can jump
}
}
public bool isGrounded(bool checkGrounded)
{
return Physics.Raycast(transform.position,-Vector3.up, distToGround +0.1);}
}
Any help will be much appreciated.