Need to change direction of transform?

I’m really confused and maybe in over my head on this AI stuff.

Here’s the skinny: I have an object that needs to know if it should turn left or right.

I have:

function GetSteer() {
var relativePoint = transform.InverseTransformPoint(path[currentPathObj].position);
newSteer = relativePoint.z;
if (newSteer > 0){
newSteer = 1;
}
else if (newSteer < 0){
newSteer = -1;
}

else if (newSteer == 0){

}
}

If newSteer = 1, the object will turn right, if it’s negative the object will turn left.

Here’s the problem:

My object’s forward isn’t the z-axis.

Here is a picture:

19819-explanation.png

(It’s an aerial view thus I left out the Y axis)

I want the object to turn towards the gray x and go towards the gray x.

The drawn axes are what my axes look like they are oriented to in unity.

Do you want to know whether the target is at left or right? If Y is your vertical direction, check the polarity of Y of the cross product of current forward and the target direction:

// get the target direction:
var targetDir: Vector3 = path[currentPathObj].position - transform.position;
targetDir.y = 0; // keep only the horizontal direction
// get 1 or -1 depending on the relative direction:
newSteer = Mathf.Sign(Vector3.Cross(targetDir, transform.forward));

If newSteer gets values opposed to what you want, just swap the arguments of Vector3.Cross.

If you want a value proportional to how much the vehicle must steer, normalize targetDir and use the cross product directly:

newSteer = Vector3.Cross(targetDir.normalized, transform.forward);

The cross product of two normalized vectors (transform.forward is normalized) is numerically equal to the sine of the angle between them.

I got it.
If i set it to the negative y axis it works.

I’m not sure why but it does what I want it to.
XD

Thanks!