Search Unity

Resolved localRotation not being called

Discussion in 'Scripting' started by laresdju, Jan 28, 2024.

  1. laresdju


    Dec 29, 2013
    Hello everyone, i am fairly new to coding and i am lost because the script is working and at the same time it isn't

    I'm trying to make a canted ADS and with this script below, everything works well, it does the rotation.
    The problem is it only works if the scene is loaded with the weapon game object active in hierarchy and I need the weapon to be disabled on scene load. So when i go pick up the weapon and load it in inventory and whenever i try to use canted sight, it doesnt work.

    I have a bool to check if it is canted, it is. localPosition and camera FOV change, but the localRotation stays the same.

    I'm trying to change Z rotation

    Vector3 cantedRot;
    and in inspector set 0, 0, 50

    Here's the script:

    Code (CSharp):
    2. /// in update()
    4.        if (Input.GetKey(KeyCode.Mouse1))
    5.         {
    6.             isAiming = true;
    8.         }
    9.         else { isAiming = false; }
    11.         if (isAiming)
    12.         {
    13.             if (Input.GetKey(KeyCode.V))
    14.             {
    15.                 isCantedAim = true;
    17.             } else
    19.             { isCantedAim = false; }
    20.         }
    21.     }
    Code (CSharp):
    1.    public void ADS()
    2.    {
    3.        if (isAiming)
    4.        {
    5.            CrosshairAim.SetActive(false);
    7.            if (isCantedAim)
    8.            {
    9.                transform.localRotation = Quaternion.Euler(cantedRot * Time.deltaTime * smoothCantRot);
    10.                transform.localPosition = Vector3.Lerp(transform.localPosition, cantedAimPos, Time.deltaTime * adsSpeed);
    11.                Camera.main.fieldOfView = 70;
    12.            }
    13.            else
    14.            {
    15.                transform.localPosition = Vector3.Lerp(transform.localPosition, aimPos, Time.deltaTime * adsSpeed);
    16.                Camera.main.fieldOfView = 75;
    17.            }
    18.        }
    19.        else
    20.        {
    21.            transform.localPosition = Vector3.Lerp(transform.localPosition, hipPosition, Time.deltaTime * adsSpeed);
    22.            CrosshairAim.SetActive(true);
    23.            Camera.main.fieldOfView = 90;
    24.        }
    25.    }
    Last edited: Jan 28, 2024
  2. Bunny83


    Oct 18, 2010
    Well, this line just makes no sense:

    Code (CSharp):
    1. transform.localRotation = Quaternion.Euler(cantedRot * Time.deltaTime * smoothCantRot);
    Why do you multiply by deltaTime here? You do a direct assignment. Multiplying by deltaTime just reduces the target rotation and also makes it jitter with the frame rate jitter.

    You probably want to use either Quaternion.RotateTowards or Quaternion.Slerp towards your desired target rotation, similar to what you did with your position.
  3. laresdju


    Dec 29, 2013
    True i messed that up, i was trying to get the smooth transition from localRotation to cantedRot like it is for position.
    That can be easily fixed.

    I tried using Quaternion.Slerp, but still, if i start the scene with the object enabled, canted ADS works fine, but if i disable the object, start scene, go pick-up the weapon, and then try to use canted ADS, it doesn't work
    localPosition and Camera FOV change except the localRotation.

    Last edited: Jan 28, 2024
  4. Bunny83


    Oct 18, 2010
    Sorry but we don't have enough context for your issue. It looks like you created a god object (a single script with way too much responsibility). Who knows what you're doing in Start / Awake or what other scripts may affect your object at the same time. Note that you have some warnings and errors in your console. You should solve them. Keep your console clean to actually see when you run into issues.

    We also don't know how your pickup system works. It seems that you just activate the gameobject that is already in the hierarchy, is that correct?

    As I said, we don't have enough context. Maybe you have something else in yours script (like a coroutine) that is fighting against your rotation. When you run it right away the coroutine may cause an error and stop running. There are literally hundreds of possible causes.
  5. laresdju


    Dec 29, 2013
    So i did some search and gone trough troubleshooting
    Turns out, having 2 scripts on one object that changes rotation makes it a problem.

    #Procedural recoil messed up that, so now i added a Empty GameObject attached recoil script on that, and continued on the gun script as i imagined, now all works fine, i am able to use canted sights and recoil as intended.

    You asking those questions helped me think, appreciate for help!

    And console error is
    Code (CSharp):
    1. Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)
    2. The following scene GameObjects were found:
    3. ItemEventManager
    Bunny83 likes this.