- Home /
Issue with Slerping
Hi there, I'm currently facing a pretty unfortunate issue. This here is my script to rotate an object up and down once again (a weapon recoil type effect) The whole thing does work, but the effects are only visible for 1-2 frames, which is not what I actually had in mind. Is there a way to make this rotation process last around one second?
public void WeaponKick (){
float kick = 2.0f;
kick = Random.Range(recoil * 1.5f, recoil * 2.0f);
kickRotation = Quaternion.Euler(item.transform.rotation.eulerAngles - new Vector3(0.0f, 0.0f, kick));
item.transform.rotation = Quaternion.Slerp(item.transform.rotation, kickRotation, 0.7f);
}
Thanks for any help, I'm pretty much lost here... :)
Answer by ProtoTerminator · Dec 18, 2016 at 03:21 PM
Do it in a coroutine:
public void WeaponKick()
{
float kick = 2.0f;
kick = Random.Range(recoil * 1.5f, recoil * 2.0f);
kickRotation = Quaternion.Euler(item.transform.localEulerAngles - new Vector3(0.0f, 0.0f, kick));
StartCoroutine(Recoil(item.transform, kickRotation, 0.5f));
}
IEnumerator Recoil(Transform trn, Vector3 kickRotation, float time)
{
Vector3 start = trn.localEulerAngles; // or use trn.eulerAngles for global rotation
float curTime = 0;
// send to kickRotation
while (curTime < 1)
{
curTime += (Time.deltaTime / time) * 2;
trn.localRotation = Quaternion.Slerp(trn.localRotation, Quaternion.Euler(kickRotation), curTime); // use trn.rotation for global rotation
yield return null; // wait until next frame
}
curTime = 0;
// send back to start rotation
while (curTime < 1)
{
curTime += (Time.deltaTime / time) * 2;
trn.localRotation = Quaternion.Slerp(Quaternion.Euler(kickRotation), trn.localRotation, curTime); // use trn.rotation for global rotation
yield return null; // wait until next frame
}
trn.localEulerAngles = start;
}
Your answer
Follow this Question
Related Questions
Quaternion.Slerp and Quaternion.Euler problem 1 Answer
Quaternion.Slerp problem... 1 Answer
Activating 3 rotations based on multiple presses on one key 2 Answers
need my object to rotate between specific angles on the x axis 2 Answers
Rigidbody spins out of control when colliding into corner 0 Answers