My rotation for the player isn’t working and I would like to get help! I’ve looked into the documentation and youtube videos but I can’t get it right for some reason.
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public float rotationSpeed = 5f;
public Rigidbody2D rb;
Vector2 moveDirection;
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
if (Input.GetMouseButtonDown(0))
{
Fire();
}
moveDirection = new Vector2(moveX, moveY);
float inputMagnitude =Mathf.Clamp01(moveDirection.magnitude);
moveDirection.Normalize();
transform.Translate(moveDirection * moveSpeed * inputMagnitude * Time.deltaTime, Space.World);
rb.velocity = new Vector2(moveDirection.x * moveSpeed * Time.deltaTime, moveDirection.y * moveSpeed * Time.deltaTime);
if (moveDirection != Vector2.zero)
{
Quaternion toRotation = Quaternion.LookRotation(transform.forward, moveDirection);
Quaternion rotate = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
rb.MoveRotation(rotate);
}
}
public GameObject bulletPrefab;
public Transform firePoint;
public float fireForce;
public void Fire()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
bullet.GetComponent<Rigidbody2D>().AddForce(firePoint.up * fireForce, ForceMode2D.Impulse);
}
}
Here is my code for the player.