- Home /
How can I make my player rotate back after key release?
Hello Unity folks,
In my 2D game I have a floating character which is the main player. If the player is floating towards the left it is leaning left till it reaches 45 degrees on the z axis of Rotation. My question is; How can I make it rotate back to start position when key is released?
This is the script to make it lean to 45 degrees in both direction;
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 1f;
private Vector3 myRotation;
void Start() {
myRotation = gameObject.transform.rotation.eulerAngles;
}
void Update() {
if(Input.GetKey(KeyCode.D))
transform.position += new Vector3
(speed * Time.deltaTime, 0.0f, 0.0f);
if(Input.GetKey(KeyCode.A))
transform.position -= new Vector3
(speed * Time.deltaTime, 0.0f, 0.0f);
if(Input.GetKey(KeyCode.D)) {
myRotation.z = Mathf.Clamp(myRotation.z - 1f, -45f, 45f);
transform.rotation = Quaternion.Euler(myRotation);
}
if (Input.GetKey (KeyCode.A)) {
myRotation.z = Mathf.Clamp (myRotation.z + 1f, -45f, 45f);
transform.rotation = Quaternion.Euler (myRotation);
}
}
}
I have tried this code to make it return to start position but it looks strange as the player is not returning slowly to its position:
if(Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D)) {
gameObject.transform.rotation = Quaternion.identity;
}
Answer by zharik86 · Feb 09, 2015 at 07:16 PM
If you want create smooth rotate back, use SLerp and see simple example below (write on CSharp):
private bool isBack = false;
void Update() {
//This your code with Getkey
if(Input.GetKey(KeyCode.D)) {
isBack = false; //in each condition, contains GetKey add this line
transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
}
//...
//Than check up key and doing smooth rotate
if(Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D)) {
isBack = true;
}
if (isBack) {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, 2.0f * Time.deltaTime);
//Check, if end rotation
if (Quaternion.Angle(transform.rotation, Quaternion.identity) < 2.0f) {
transform.rotation = Quaternion.identity;
isBack = false;
}
}
}
But, better use always Quaternion instead eulerAngle. I hope that it will help you.
Thanks for the quick reply :) I have added it in my script but nothing has changed. Can u see what I have done wrong?
using UnityEngine;
using System.Collections;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public float speed = 1f;
private Vector3 myRotation;
private bool isBack = false;
void Start() {
myRotation = gameObject.transform.rotation.eulerAngles;
}
void Update() {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
transform.position += new Vector3
(speed * Time.deltaTime, 0.0f, 0.0f);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
transform.position -= new Vector3
(speed * Time.deltaTime, 0.0f, 0.0f);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
myRotation.z = $$anonymous$$athf.Clamp(myRotation.z - 1f, -45f, 45f);
transform.rotation = Quaternion.Euler(myRotation);
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A)) {
myRotation.z = $$anonymous$$athf.Clamp (myRotation.z + 1f, -45f, 45f);
transform.rotation = Quaternion.Euler (myRotation);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
isBack = false; //in each condition, contains Get$$anonymous$$ey add this line
transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
isBack = false; //in each condition, contains Get$$anonymous$$ey add this line
transform.position -= new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
}
//...
//Than check up key and doing smooth rotate
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.D)) {
isBack = true;
}
if (isBack) {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, 2.0f * Time.deltaTime);
//Check, if end rotation
if (Quaternion.Angle(transform.rotation, Quaternion.identity) < 2.0f) {
transform.rotation = Quaternion.identity;
isBack = false;
}
}
}
}
}
}
What you meen, say nothing change? Rotate not smooth or not rotate. I think, I write you code, base your example, when your player smooth rotate and when Get$$anonymous$$ey and when Get$$anonymous$$eyUp; without eulerAngles.
public float speed = 1.0f;
public float speedRot = 1.0f;
private bool isBack = false;
void Update() {
//$$anonymous$$ove right
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) {
isBack = false;
transform.position += new Vector(speed * Time.deltaTime, 0.0f, 0.0f);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, -45.0f), speedRot * Time.deltaTime);
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) {
isBack = false;
transform.position -= new Vector(speed * Time.deltaTime, 0.0f, 0.0f);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, 45.0f), speedRot * Time.deltaTime);
}
//Than check up key and doing smooth rotate
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.D)) {
isBack = true;
}
if (isBack) {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, speedRot * Time.deltaTime);
//Check, if end rotation
if (Quaternion.Angle(transform.rotation, Quaternion.identity) < 2.0f) {
transform.rotation = Quaternion.identity;
isBack = false;
}
}
}
O$$anonymous$$G zharik U ARE $$anonymous$$Y HERO!!! I was looking so long for this solution :D Thank you so much!