Hi all,
I need to find out whether an object is on the left or right of a moving rigidbody. I setup at test project with just 2 cubes to figure out how to solve this. cube2 is moved manually in the scene editor while running the scene). Maybe I am making things over complicated but I thought the below should give the right result and I can´t think of another way to solve this.
// get direction of cube2
cube2dir1 = cube2.position - oldPos;
// switch x and z values
cube2dir2 = Vector3(cube2dir1.z, cube2dir1.y, cube2dir1.x);
// get vector between cubes
cubesdir = cube1.position - cube2.position;
if (Vector3.Dot(cube2dir2,cubesdir) < 0)
print ("on the left" + Vector3.Dot(cube2dir2,cubesdir));
else
print ("on the right" + Vector3.Dot(cube2dir2,cubesdir));
oldPos = cube2.position;
It does work right when moving cube2 over the z-axis, it gives opposite results when moving the cube over the x-axis. When, for example rotating cube2 45 degrees and the result changes when the angle between the moving direction of cube2 and cube1 and the vector between cube1 and cube2 is 90 degrees.
I am not sure where this goes wrong or what else to try. Anyone knows how to do this?
Many thanks,
Raoul