- Home /
horizontal recoil reset with slerp
Hello! I am currently trying to implement horizontal-only recoil to my weapon. For that, I rotate the weapon after shooting either to the left or to the right. After I am done shooting, I want to rotate back with using the slerp function. However, I cant figure out how something like that would work. I tried saving the position in a quaternion, but that created another problem, where if you would shoot and move your mouse horizontally, it would just revert your rotation to the old rotation, sometimes even rotating the character by 180°. My code can be found below. I don't expect you to provide code, just instructions are highly appreciated too!
void Start()
{
originalPosition = transform.rotation;
}
void Update() {
while (slerpYes && transform.rotation != originalPosition)
{
transform.rotation = Quaternion.Slerp(transform.rotation, originalPosition, 6f * Time.deltaTime);
Debug.Log("Test");
}
StartCoroutine(Recoil());
}
private IEnumerator Recoil()
{
slerpYes = false;
int resetTimer;
if(Input.GetMouseButtonDown(0))
{
resetTimer = Random.Range(0, 2);
if (resetTimer == 0)
{
transform.Rotate(0, 1.75f, 0);
}
else if (resetTimer == 1)
{
transform.Rotate(0, -1.75f, 0);
}
yield return new WaitForSeconds(0.15f);
slerpYes = true;
Debug.Log("Test2");
}
}
Answer by Eno-Khaon · Oct 14, 2018 at 03:26 AM
I'm assuming this is a first-person shooter you're describing, so this answer works with that in mind.
The most straightforward approach would probably be to add an empty GameObject controller as a parent to your camera (again, assuming first-person shooter), then use its Transform for modifiers related to recoil. Whenever it's inactive (you're not firing), its localRotation would be none, or Quaternion.identity.
When you fire, nudge the localRotation of that empty GameObject to be off-center, then ease it back toward the original rotation. Then, you won't need to worry about any player-made changes to rotation during that time.
First of all, thank you for taking the time to help me out. I tried to implement the things that you wrote about, but this is a first for me, so I probably did it wrong:
Quaternion originalPosition;
bool slerpYes;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if(slerpYes && transform.localRotation != Quaternion.identity)
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, 15f);
}
StartCoroutine(WeaponRecoil());
}
private IEnumerator WeaponRecoil()
{
slerpYes = false;
int resetTimer;
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
resetTimer = Random.Range(0, 2);
if (resetTimer == 0)
{
transform.Rotate(0, 1.75f, 0);
}
else if (resetTimer == 1)
{
transform.Rotate(0, -1.75f, 0);
}
yield return new WaitForSeconds(0.15f);
slerpYes = true;
Debug.Log("Test2");
}
}
What you're seeing here is the gameObject Controller that you spoke about. And I made it so that the localRotation would go back to "Quaternion.identity" after i stopped shooting. $$anonymous$$y problem is now, that the weapon itself doesnt move anymore, but the bullet do seem to go into different directions. I would really appreciate some additional input from you. I am definitely going to try to figure it out myself though. Still, thank you very much.
Oh, and im the guy that created this question. I somehow cant log into my other account, just so you know.
At the very least, there's this line:
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, 15f);
Your interpolation will cap at 1, so 15 isn't really helping there. I assume you meant to multiply it by Time.deltaTime like the previous time?