0
\$\begingroup\$

I am trying to get a good camera recoil system in Unity, but what I have tried so far doesn't seem to look very good. My current method, is using Quaternion.Lerp. With the way I have my shooting system set up, the camera recoil going up all happens within one frame, so when you shoot faster, it tends to look choppy, whereas the recoil going back down is smooth and normal. My question is, is there a better way to handle the camera recoil, while keeping the same look and effect, but having it look much more smooth? I have a quick video to show how the camera recoil looks (watch as the camera moves upwards) https://www.youtube.com/watch?v=HsS0rtoj9l4

And here is the part of my code that handles the camera recoil

void Update () {
    Recoil2();

    Vector3 cameraRecoil = weaponSettings.CurrentWeapon.cameraRecoil;
    Transform cameraClimb = generalSettings.cameraClimbHolder.transform;
    string fireMode = weaponSettings.CurrentWeapon.fireMode.ToString();

    if (fireMode == "Semi")
        if (Input.GetButtonDown("Fire1"))
            Shoot();
        else
            CameraRecoil1 = Vector3.Lerp(CameraRecoil1, new Vector3(0, 0, 0), Time.deltaTime * cameraRecoil.z / 10f);

    if (fireMode == "Auto")
        if (Input.GetButton("Fire1"))
            Shoot();
        else
            CameraRecoil1 = Vector3.Lerp(CameraRecoil1, new Vector3(0, 0, 0), Time.deltaTime * cameraRecoil.z / 3f);
}

void Shoot ()
{
    if (shoottime <= Time.time && !isBusy && !isReloading && !isRunning)
    {
        shoottime = Time.time + weaponSettings.CurrentWeapon.fireRate;

        WeaponInfo currentWeapon = weaponSettings.CurrentWeapon;
        Transform cameraClimb = generalSettings.cameraClimbHolder.transform;
        Vector3 cameraRecoil = weaponSettings.CurrentWeapon.cameraRecoil;

        CameraRecoil1 = Vector3.Lerp(CameraRecoil1, new Vector3(-cameraRecoil.x, Random.Range(-cameraRecoil.y, cameraRecoil.y), 0), Time.deltaTime * cameraRecoil.z);
    }
}

void Recoil2 ()
{
    Vector3 cameraRecoil = weaponSettings.CurrentWeapon.cameraRecoil;
    Transform cameraClimb = generalSettings.cameraClimbHolder.transform;

    cameraClimb.localEulerAngles = CameraRecoil1;
}

Anyone have any ideas? I appreciate anyone who can help me!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Your use of Vector3.Lerp seems out of place. It is intended to find a point along a line as a percentage distance from 0 to 1.

What you may be looking for instead is Vector3.MoveTowards or Vector3.SmoothDamp.

Also, when initially creating the recoil inside of Shoot(), I would assume you'd want to set CameraRecoil1 to a value independent of its current value, rather than Lerping (or MoveTowardsing) it.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .