Hi, i’m trying to make an object that can rotate on the x axis back and forth 180 degrees and whilst the player is not pushing the rotate button and if it’s not level(which is a 0 value) to lerp back into a level position.
However with my code it moves back instantly to a level position, am i using lerp wrong? is there a different way i can achieve this without using a for loop?
Here’s my code
using UnityEngine;
using System.Collections;
public class PlayerInput : MonoBehaviour {
public float moveSpeed;
private float horizontal;
private float verticle;
private float rotate;
private Transform startingTransform;
void Awake(){
startingTransform = transform;
}
void Update(){
//get axis values
horizontal = Input.GetAxis ("Horizontal") * moveSpeed;
verticle = Input.GetAxis ("Vertical") * moveSpeed;
rotate = Input.GetAxis ("Vertical") * moveSpeed;
//make time constant
horizontal *= Time.deltaTime;
verticle *= Time.deltaTime;
//translate player
transform.Translate(horizontal,0,0,Space.World);
transform.Translate(0,verticle,0,Space.World);
//rotate character model within 180 degrees
if(transform.rotation.x < 0.5f && transform.rotation.x > -0.5f){
transform.Rotate(rotate,0,0);
}
//if the player is not leveled out and is not rotateing start the rotate them back.
if(transform.rotation.x != 0.0f && Input.GetAxis ("Vertical") == 0.0f){
float angle = Mathf.LerpAngle(360,startingTransform.rotation.x, 100 * Time.deltaTime);
transform.eulerAngles =new Vector3(angle,0,0);
}
}
Thanks for your time.