I am trying to have a smooth rotation animation on this hover-car to make it seem more realistic.
I am trying to code it so that if the key a
or d
is pressed it will rotate to the left and right accordingly. If it rotates too far then it dies and the scene is reloaded. When the keys a
or d
are lifted then it will smoothly revert back to its original position. I want it to rotate on the x axis
of its origin. If you see in the image the scenes axis is z
and the axis of origin that points in the direction of that from the vehicle is its x
.
Here is the scene with the axis's of the vehicle:
public int speed = 100;
public float maxrotation = 30f; //To Set Max Rotation
void Start()
{
}
// Update is called once per frame
void Update()
{
if (transform.rotation.eulerAngles.x > 0)
{
transform.rotation.eulerAngles.x - 1; //there is an error on this line saying "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement"
}
if (transform.rotation.eulerAngles.x < 0)
{
transform.rotation.eulerAngles.x + 1; //there is an error on this line saying "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement"
}
if (Input.GetKey(KeyCode.A))
{
//Set a rate at which we should turn
float turnSpeed = speed * Time.deltaTime;
//Connect turning rate to horizonal motion for smooth transition
float rotate = Input.GetAxis("Horizontal") * turnSpeed;
// //Get current rotation
// float currentRotation = gameObject.transform.rotation.z;
// //Add current rotation to rotation rate to get new rotation
// Quaternion rotation = Quaternion.Euler (0, 0, currentRotation + rotate);
// //Move object to new rotation
// gameObject.transform.rotation = rotation;
gameObject.transform.Rotate(Vector3.left * rotate);
}
if (Input.GetKey(KeyCode.D))
{
//Set a rate at which we should turn
float turnSpeed = speed * Time.deltaTime;
//Connect turning rate to horizonal motion for smooth transition
float rotate = Input.GetAxis("Horizontal") * turnSpeed;
// //Get current rotation
// float currentRotation = gameObject.transform.rotation.z;
// //Add current rotation to rotation rate to get new rotation
// Quaternion rotation = Quaternion.Euler (0, 0, currentRotation + rotate);
// //Move object to new rotation
// gameObject.transform.rotation = rotation;
gameObject.transform.Rotate(Vector3.left * rotate);
}
if (transform.eulerAngles.x > maxrotation)
{
Application.LoadLevel(Application.loadedLevel); //kills player and reloads scene
scorecript.scoreValue = 0; // sets score to zero
}
if (transform.eulerAngles.x < maxrotation - (maxrotation * 2))
{
Application.LoadLevel(Application.loadedLevel); //kills player and reloads scene
scorecript.scoreValue = 0; //sets score to zero
}
}
<!-- end snippet -->