I was wondering how I could make my player rotate towards the user input smoothly, I’ve been using unity for some years but I still don’t get the rotation and quaternion things so I thought you may be able to help.
The ship moves automatically to where it’s looking at a constant speed but I can’t seem to figure out how to make it so that when you press “A” for example the ship automatically rotates towards 270 degrees and so on, which I feel would be much more intuitive than what I have now that simply rotates one way or another depending on the key you are pressing. It would be nice to also have some way of implementing it with “Input.GetAxis(“Horizontal”)” or something like that since I would like to adapt it to a ps4 controller instead of limiting it to just a keyboard
Thanks in advance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipController : MonoBehaviour
{
public float speed = 5;
public float rotationSpeed = 20;
Rigidbody2D rb;
BoxCollider2D Bcoll;
// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
Bcoll = gameObject.GetComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
gameObject.transform.Translate(-Vector3.up * Time.deltaTime * speed);
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(0, 0, Time.deltaTime * rotationSpeed);
}else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, 0, -Time.deltaTime * rotationSpeed);
}
}
}