I’ve been tooling with a few movement options, but at the moment I’m not sure how to set it up correctly.
I’m calling my Move() function in Update, the plan is to have the player rotate towards the key input, and be moving forward while there is input. Transform.Translate doesn’t allow for easy physics integration, so I was hoping someone could point out what I’m missing in the code.
Player has non kinematic rigidbody and box collider.
Solid object has non kinematic rigidbody and box collider, and is tagged ‘Solid’.
private void Move()
{
float horizontal = Input.GetAxisRaw("p" + playerId + "MoveHorizontal");
float vertical = Input.GetAxisRaw("p" + playerId + "MoveVertical");
Quaternion targetAngle;
if (horizontal != 0 || vertical != 0)
{
float angle = Mathf.Atan2(vertical, horizontal) * Mathf.Rad2Deg;
targetAngle = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetAngle,
Time.deltaTime * rotationSpeed);
Vector3 fwd = transform.TransformDirection(Vector3.forward);
rigidbody.velocity = transform.right * Time.deltaTime * movementSpeed;
}
else
{
rigidbody.velocity = transform.right * 0;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Solid")
{
rigidbody.velocity = transform.right * 0;
}
}