Get value of an object left or right of another object?

A is a fishing rod tip, B is a fish. I’m trying to bend the rod towards the fish.

How can I find out the distance from object A to object B? Object B moves either left or right of object A and I need to know the value in a float or vector.

1388910--71219--$angle.jpg

Any suggestions please?

Thanks

The Vector3 struct has a static method which may help you with this:

var distance = Vector3.Distance(positionOfRodTip, positionOfFishy)

Hi derkoi,
Let me give this a go, please bear in mind I’ve done this in other 3d packages but not in Unity; unity may have a built in thing.
Plus I would love to know how pro game devs approach this problem :slight_smile:

Given A and B, with A having a “forward” vector of sorts (basically a vector indicating the forward direction for A, pointing up)
Take a cross product between A’s forward vector and the vector (B->A). So, we now have a new vector pointing away from us, into the screen.
Again, take the cross product between the new vector and A’s forward vector. This will now give a vector pointing to the right (from the pov of our screens).

Now, if you do a dot product between this vector pointing to the right and vector (B->A), if the dot product’s result is positive, then B is to the right of A, and if it is negative, to the left. Zero values indicate it is at a right angle to A, and another dot product with the forward vector and (B->A) can be performed to determine if it is pointing in the same direction as A or away from it.

HTHs!

@Marrk - the distance method returns the distance between two points; OP needs to know if a point is to the right or left of a vector.

Just get the position in the rod’s local space.

Thanks for the replies.

How do you do that without objects being parented?

pointOnObjectB_rodSpace = rodTransform.InverseTransformPoint(pointOnObjectB_worldSpace)
relativeToTip = pointOnObjectB_rodSpace - rodTipPoint_rodSpace

rodTipPoint_rodSpace is a value you can cache.