How to test a rotation threshold?

I want to pick up an object, walk over, and place it in a target location. So if the object is within some threshold of position and rotation of the target objects, it snaps to that position/rotation.

I’m using Vector3.distance to test how close they are but can’t seem to figure out how to test if the object is oriented correctly.

Here’s my code for that:

public GameObject target;
private float xMax;
private float xMin;
private float yMax;
private float yMin;
private float zMax;
private float zMin;
public float margin = 5.0f;

void Start () {
  xMax = target.transform.rotation.x + margin;
  xMin = xMax - (margin * 2);
  yMax = target.transform.rotation.y + margin;
  yMin = yMax - (margin * 2);
  zMax = target.transform.rotation.z + margin;
  zMin = zMax - (margin * 2);
}

void Update () {
  float x = transform. rotation.x;
  float y = transform. rotation.y;
  float z = transform. rotation.z;
  if (x < xMax && x > xMin) {
    if (y < yMax && x > yMin) {
      if (z < zMax && x > zMin) {
        transform.position = target.position;
        transform.rotation = target.position;
      }
    }
  }

I’m almost positive there’s a better way to code this.

Can anyone tell me the best way to compare two transforms and, if object A is within some threshold of object B, snap object A to object B?

Thanks in advance.

I like to put a logical sphere in front of the player (or a circle in a 2D game).

Basically the position of that circle is updated each frame to be a certain distance in front of the player, like so:

Vector3 center = transform.forward * 1.0f;

From there you can check if anything is close enough to that center that it is probably reachable, then take further action.

if (Vector3.Distance( target.transform.position, center) < 0.5f)
{
  Debug.Log( "Your 'touch sphere' is close enough now to snap you to the target.");
  // this will snap to the object:
  transform.LookAt( target.transform.position);
}

Yeah, I think I have a handle on the distance/position using Vector3.distance. I’m asking how to verify orientation (within a threshold of, say, 5 degrees).

This is a VR app so when you are holding something in your hand, it can be at basically any orientation. So when you are placing the held object in its target location, I need to verify both A) the distance between the held object and the target object and B) the orientation of the held object relative to the target object (to make sure you aren’t putting it in sideways or something).

Are you just looking for Vector3.Angle()? That will take any two arbitrary vectors and tell you the angle between them.

I think he may want Quaternion.Angle instead, so he can directly measure the difference between two orientations.

1 Like

You’re totally right Joe, I was mis-thinking that he had pointing directions.

Ah! Thank you both!

Here’s the cleaned up code for those who come across this thread later:

public GameObject target;
    public float distance = 0.0254f;
    public float angle = 3.0f;

    void Update () {
        if (Vector3.Distance (this.transform.position, target.transform.position) < distance && Quaternion.Angle (this.transform.rotation, target.transform.rotation) < angle) {
            this.transform.parent = null;
            this.GetComponent<Rigidbody> ().isKinematic = true;
            this.transform.position = target.transform.position;
            this.transform.rotation = target.transform.rotation;
        }
    }
1 Like