- Home /
Question by
LordOfKspModding · Mar 12, 2020 at 06:02 PM ·
c#3dlerpeulerangleseuler
Lerping glitches out in one way, but not the other
I am using Mathf.LerpAngle in order to open and close a door. When I open it, it works fine. However, when I close it, it begins to move more open, and eventually glitches out if I open it again. This is the code I have:
void Update()
{
if(transform.eulerAngles.z > 50 || transform.eulerAngles.z < -50){
doorClosed = false;
}
if(Input.GetKeyDown("e")){
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit)){
if(hit.transform == gameObject.transform){
currentAngle = transform.eulerAngles;
start = Time.time;
if(doorClosed){
StartCoroutine(onTouch(85f));
doorClosed = !doorClosed;
}
else{
StartCoroutine(onTouch(0f));
doorClosed = !doorClosed;
}
}
}
}
}
IEnumerator onTouch(float targ){
while(true){
yield return null;
float t = Time.time;
Vector3 targetRot = new Vector3(transform.rotation.x, transform.rotation.y, targ);
currentAngle = new Vector3(
currentAngle.x,
currentAngle.y,
Mathf.LerpAngle(currentAngle.z, targetRot.z, t/dur)
);
Debug.Log("Changing Door");
t+= Time.deltaTime;
transform.rotation = Quaternion.Euler(currentAngle);
if(t > dur){
doorClosed = !doorClosed;
break;
}
}
}
I have no idea why and have tried everything.
Comment
Add Debug.Log(t/dur) is the value between 0 and 1? Just wondering because t is set to Time.time but then constantly increased by Time.deltaTime. Try setting t to 0 first.
Your answer
Follow this Question
Related Questions
Camera rotation behaving strangely 2 Answers
Mathf.Lerp won't stop snapping 2 Answers
Rotation Lerp 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers