# Left/right, up/dwn of me detection

I intend my logic to be like this.
If target is right of me I rotate 2deg/frame right, otherwise I rotate left.

To do this I need to know if target is left of me or right of me.

What I have so far is the script below the ------ what happens when I use this script is my object just rotates around and around continuously

More complete info for why I am doing things this way and my goal:

I am making my own turret from scratch because it will have to be constrained within certain limits while a child of a rigidbody. Lookat and slerp use quaternions for rotations which are too hard for me to modify. More completely what I am trying to accomplish is here Constrain Turret Rotation while child of Rigidbody - Questions & Answers - Unity Discussions

``````var target : Transform;
var turnspeed: float = 5;

function Update()
{

var targetDir = target.position - transform.position;
var forward = transform.forward;

var angle = Vector3.Angle(targetDir, forward);

if (angle <= 5.0)
// print("close");

//var angle = 0.0;
//var axis = Vector3.zero;
//transform.rotation.ToAngleAxis(angle, axis);

// detects if other transform is left of this object
var right = transform.TransformDirection(Vector3.right);
var toOther = target.position - transform.position;
if (Vector3.Dot(right,toOther) < 0){
// print ("other transform is left");
//transform.Rotate(Vector3.right * Time.deltaTime);
}

var up = transform.TransformDirection(Vector3.up);
if (Vector3.Dot(up,toOther) < 0){
//  print ("other transform is up");
transform.Rotate(Vector3.right * Time.deltaTime * turnspeed * -1);
}
if (Vector3.Dot(up,toOther) > 0){
transform.Rotate(Vector3.right * Time.deltaTime * turnspeed * -1);
}
}
``````

“If target is right of me I rotate 2deg/frame right, otherwise I rotate left.”

Not a good idea. This way on a fast computer it could go an entire circle in a second, and on a slow computer it might take 20 seconds. You’ll want to rotate per second, not per frame.

So, how do you see if something it to the right of… something? You use Transform.InverseTransformPoint( ) this Transforms position from world space to local space, so where the input Vector3(1,1,1) means 1 right, up and forward from the center of the world, the output Vector3(1,1,1) would mean 1 right, up and forward from the Transform you used.

So say you have the GameObject player, you take it’s transform: player.transform and you feed it the InverseTransformPoint function with the position of the enemy: player.transform.InverseTransformPoint(enemy.transform.position).

This will result in a Vector3 with the relative position of the position you fed it to the player. So Vector3(2,3,4) will mean 2 right, 3 up and 4 right of the player. So to check if it’s to the right you want to check if the relative Vector3.x is larger then 0. So:

``````if( player.transform.InverseTransformPoint(enemy.transform.position).x > 0 ) {
``````

Debug.Log(“the enemty is to the right of the player”);
}

Combining what Joshua says and the script refrence I have made the script below. It is not a complete autoturret, that requires some extra stuff “like when to fire” and “is target close enough”. However this will do its best to turn towards a target and stay facing him while connected to a rigidbody but constrained to rotate only about the y axis so it works just great for a tank’s turret. A con is that it quivers a little when if has found the target but that can be easily fixed in the script’s logic or perhaps the turning speed.

works good for this heirarchy and nothing looks wierd.

parent rigidbody(tank body) → turret on top(apply script here)

``````var target : Transform;
var turnspeed: float = 5;

function Update()
{

var targetRelative = transform.InverseTransformPoint(target.transform.position);

if (targetRelative.x > 0){
transform.Rotate(0,turnspeed * Time.deltaTime,0);
}
else{
transform.Rotate(0,turnspeed * Time.deltaTime*-1,0);
}
}
``````