Problem in LERP Rotaion Based on InputField Value
Hi All,
I am trying to do Lerp rotation animation in x axis by getting values from InputField. During runtime, Everytime I type a value in InputField and press Enter, I call a function to update the latest Value to the class. The GameObject should rotate and stop as per the value. Here's how I coded. Not works as expected. Could you Help me:
public class getInputFocus : MonoBehaviour {
public GameObject rotPlate;
private float inputValue;
private Vector3 rotateTo;
private float rotSpeed = 2.0f;
void Update () {
if (rotPlate.transform.eulerAngles.x != rotateTo.x) {
rotPlate.transform.eulerAngles = Vector3.Lerp (rotPlate.transform.eulerAngles, rotateTo, Time.deltaTime*rotSpeed);
}
}
public void getInput(string UserInput) // From InputField
{
Debug.Log (UserInput);
inputValue = float.Parse (UserInput);
//User values should be within 0 to 100
if((inputValue <= 100) && (inputValue >= 0))
{
//Convert values into Angle Because roation angle should be 0-90
float newValue = (inputValue / 100) * 90;
rotateTo.x = newValue;
}
}
}
I had tried it and the rotation is working. Do you set the getInput as the handler for the End Edit event of the InputField?
Hi @Yword Thanks for the reply. Yes I set the getInput as End Edit only. But It works partially. Like, if we give any value from 0 to 100 it rotates to the value we've given. But if we give 100, then I am not able to rotate it from 100th position to 90, 80, 70, 60... , if we give 0 or 10 only it rotates again. What may be the reason? How to solve this. Could you help
Answer by Yword · Dec 08, 2015 at 02:51 PM
I think this is the problem of gimbal lock. Maybe you can try Quaternion.Lerp as follows:
void Update ()
{
Quaternion targetRotation = Quaternion.Euler(rotateTo);
if (!Mathf.Approximately(Quaternion.Angle(rotPlate.transform.rotation, targetRotation), 0))
{
rotPlate.transform.rotation =
Quaternion.Lerp(rotPlate.transform.rotation, targetRotation, Time.deltaTime * rotSpeed);
}
}