- Home /
how do I rotate the player to face the user input smoothly?
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 :D
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);
}
}
}
Also as a tip, getting comfortable with Quaternions will be really helpful in the future. As the unity site also mentions, it not easy to understand what a quaternion is intuitively. But it will greatly increase the coding fun when you know what it is, what it does and how you can use it. I suggest just watching a couple of youtube explanations. :)
Answer by metalted · Mar 17, 2020 at 09:04 PM
So this will be a lot easier when using the horizontal and vertical axis instead of WASD, because you already have the axis going from -1 to 1. Its hard to explain how it works, so ill just write some code and comment a long the way:
//Set a speed for the rotation
float rotationSpeed = 5f;
//Get the position of the player.
Vector3 currentPosition = transform.position;
//Create a vector for the inputs (Which components you use depends on the game)
Vector3 inputVector = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
//Adding these vectors together will result in a position in the world, that is around your player.
inputVector += currentPosition;
/*Example:
* The players position is at 0,0,0.
* Input is 1,0,0.
* The position in space would be 0,0,0 + 1,0,0 = 1,0,0.
* This would be to the right of the player.
*/
//Now we create a target rotation, by creating a direction vector: (This would be just be inputVector in this case).
Quaternion targetRotation = Quaternion.LookRotation(inputVector - currentPosition);
//Rotate smoothly to this target:
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed);
Im not at home at the moment, so i can't really test it properly, so it might not work as is. But im pretty sure this will get you in the right direction.
Your answer
Follow this Question
Related Questions
How to control a 2d character in an top-down android game with the standard joystick? 1 Answer
Rotating a 2D object when the phone is rotated 1 Answer
2D spaceship rotation 0 Answers
Why Rotate player toward mouse direction keeps increasing rotation speed? 1 Answer
how to rotate 2d obj on android 1 Answer