My object keeps sliding after transforming it but only on one axis, can someone help me find out why?
moving left and right makes it slide continious in the direction it was going
and going up and down does not…
public class Player : MonoBehaviour
{
private float turnSpeed = 15f;
private float moveSpeed = 1f;
private Rigidbody rigidbody;
private Collider collider;
public void Awake()
{
rigidbody = GetComponent<Rigidbody>();
collider = GetComponentInChildren<Collider>();
}
private void FixedUpdate()
{
float v = Input.GetAxisRaw("Vertical");
float h = Input.GetAxisRaw("Horizontal");
if (
Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.W) ||
Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
{
Move(h, v);
}
}
private void Move(float horizontal, float vertical)
{
//Rotating(horizontal, vertical);
MovePlayer(horizontal, vertical);
}
void MovePlayer(float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
transform.position += targetDirection * moveSpeed * Time.deltaTime;
}
void Rotating(float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSpeed * Time.deltaTime);
rigidbody.MoveRotation(newRotation);
}
}