Search Unity

Discussion Code review this piece of code please (Rotate two transforms in relation to eachother)

Discussion in 'Scripting' started by Max-om, Dec 9, 2022.

  1. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    In our VR game you can attach magazines to weapons. For the revolver there is a special case. The magazine entry port on the revolver needs to be rotated to the closest match so that the hand on the speedloader doesnt offset too much which looks strange). I came up with this code that uses SetParent so both transforms end up in same local space. Do you guys have any cleaner approuches?

    Code (CSharp):
    1.         private void BegunAttachMagazine(InteractableMagazine magazine)
    2.         {
    3.             if (!firearm.NetworkedFirearm.IsOwner) return;
    4.  
    5.             IgnoreInternalCollision(magazine.Colliders, true);
    6.  
    7.             firearm.MagazineEntryDetector.transform.localRotation = Quaternion.identity;
    8.             magazine.transform.SetParent(firearm.MagazineEntryDetector.transform);
    9.  
    10.             var minDelta = float.MaxValue;
    11.             var targetRotation = Quaternion.identity;
    12.  
    13.             for (int i = 0; i < BulletContainer.Capacity; i++)
    14.             {
    15.                 var rotation = Quaternion.Euler(0, 0, i * bulletAngleDelta);
    16.                 var delta = Quaternion.Angle(magazine.transform.localRotation, rotation);
    17.  
    18.  
    19.                 if (delta < minDelta)
    20.                 {
    21.                     targetRotation = rotation;
    22.                     minDelta = delta;
    23.                 }
    24.             }
    25.  
    26.             magazine.transform.SetParent(null);
    27.             firearm.MagazineEntryDetector.transform.localRotation = targetRotation;
    28.         }
     
  2. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Yeah.

    revolver rotates on a single axis. There is a less compute solution for you if you’d care for it if you’d care for it it’s an Euler solution and does not involve quaternions, tell me and I will write up an example for you. If not for the record that’s the shortest quaternion use case I’ve seen in a while
     
  3. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    Shoot! :D Just note that the player can enter the trigger with the speedloader slighly rotated etc. Above code finds the closest match regardless
     
  4. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    To clarify what I ment, on this line

    Code (CSharp):
    1. var delta = Quaternion.Angle(magazine.transform.localRotation, rotation);
    rotation is rotated on one axis. But magazine.transform.localRotation is not
     
  5. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Code (CSharp):
    1.  
    2.         ///Define this once
    3.         Transform GUN = new GameObject().transform; // YOUR GUN
    4.         bool GUN_HAS_FIRED = true; /// DID YOU JUST SHOOT?
    5.         float BULLET_CHAMBERS = 6; // 60 nice and simple HOW BIG WAS THIS GUN?
    6.         float REVOLVER_CYLINDERS = (360 / BULLET_CHAMBERS); // Spacing between the chambers
    7.         Vector3 SETTER = Vector3.zero;
    8.         int INTERVAL = 1; //WHICH CHAMBER ARE WE SHOOTING
    9.         ////////////////////
    10.         if (GUN_HAS_FIRED)
    11.         {
    12.             INTERVAL = INTERVAL + 1;
    13.             if (INTERVAL > BULLET_CHAMBERS)
    14.                 INTERVAL = 1;
    15.         }
    16.         if (GUN.transform.localEulerAngles.zxy != (REVOLVER_CYLINDERS * INTERVAL))
    17.         {
    18.             SETTER = SETTER + new Vector3(0, 0, 1);
    19.             GUN.transform.localEulerAngles = SETTER;
    20.         }
    21.         //WE assume chamber has a parent object that is attached to the FPS camera.
    22.         //Any axis will work. zxy was used to depict this.
    23.  


    Ignore the hierarchy of this old piece of crap project :) It was the gun I was focussed on really.
     
  6. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    I visualised the problem here