Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Comparing two game objects using Quaternion.angle help.

Discussion in 'Scripting' started by ymhilar, Aug 3, 2023.

  1. ymhilar

    ymhilar

    Joined:
    Feb 8, 2023
    Posts:
    5
    I am trying to snap an objec1t to another object2 of similar shape. I want to have a threshold on when obj1 can snap onto obj2, maybe when obj1 is almost the right rotation and orientation. I was trying to use Quaternion.angle(obj1, obj2) but I'm not sure I understand the function completely. Below is my script, where anglePrecision is the threshold I want to hit. When I put anglePrecision anywhere lower 50, it never snaps. 90 is way too far from the correct orientation/rotation. Want to have it be customizable.

    Note: Doing all this in VR.

    upload_2023-8-3_11-4-59.png

    upload_2023-8-3_11-3-35.png
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    581
    Try putting the code in OnTriggerStay instead and use:

    Code (CSharp):
    1. float difference=Vector3.Angle(transform.up,other.transform.up);
     
    ymhilar and wideeyenow_unity like this.
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I think you might want https://docs.unity3d.com/ScriptReference/Collider.OnTriggerStay.html rather than just enter?

    But I always thought people did this by distance, while keeping the camera.forward position, but if the objects matched and the distance was within the threshold to the ghost, the alive object would snap to ghost position/rotation. Until camera.forward position was out of threshold, then object would go back to free position.

    But that's regardless if you mean to also add in rotations, in order for it to snap. So I'm thinking you'll need to convert the Quats to Vectors(euler), and find your min/max in the 0-360 aspect, and set the allowance appropriately of each rotation(x, y, z).

    As I say it, I think there is a better way. But I just know asking for proper values from quaternions never goes well, in the handling of gimbal lock. So it never changes from 90/-90 before going back down to 0 again. But Euler at least gives you 360/-360 to work with.

    Hopefully someone has a better idea :rolleyes:
     
    ymhilar likes this.
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    581
    Yep, that's how I would do it. Triggers seems a little clunky for this..
     
  5. ymhilar

    ymhilar

    Joined:
    Feb 8, 2023
    Posts:
    5
    I will try this, thank you very much!
     
  6. ymhilar

    ymhilar

    Joined:
    Feb 8, 2023
    Posts:
    5

    I see, i will look into this as well! I was working off a teammate's original script and we're both new to unity so this is a good piece of knowledge. Thank you so much!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    Rather than angles you could always try and snap the three cardinal vectors when they get close enough.

    The shortcuts transform.right, transform.up and transform.forward are available on all Transforms.

    If you subtract them from each other (they are unit vectors) you could consider an object aligned when all three are sufficiently small magnitude, and all three positions are sufficiently small magnitude, then just snap.