- Home /
How to make rotation return to starts after key release? #c
Hello everybody,
I am struggling with a problem. my game character which is floating is able to lean 45 degrees to the left when going left and vice versa. my question is how can i return the rotation on the z axis to start position(0 degrees) in a slow movement? i have been working with a code which is not solving the problem. it is not giving any faults but strange things occurs.
the code i have now to make it go leaning to 45 degrees is:
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);
}
}
}
the code i was working with is:
if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A)) {
gameObject.transform.rotation = Quaternion.identity; }
what should i apply to my first script or change to the script above?
any recommendation would be appreciated.
thanks in advance!
Answer by Steve L · Feb 10, 2015 at 01:22 PM
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed = 5.0f;
public float speedRot = 5.0f;
private bool isBack = false;
void Update() {
//Move right
if (Input.GetKey (KeyCode.D)) {
isBack = false;
transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, -45.0f), speedRot * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A)) {
isBack = false;
transform.position -= new Vector3 (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.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.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;
}
}
} }
Found it myself ;)
Your answer
Follow this Question
Related Questions
How to make Camera position Independent of its Rotation? 1 Answer
Unfreezing rotation causes the position and rotation go crazy 0 Answers
Problem with rotation of objects 0 Answers
Firing towards mouse- aim in all directions? 1 Answer
navmesh agent updatePosition updateRotation: what's this? 1 Answer