- Home /
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:
(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.
What is your objects forward then? Just use that ins$$anonymous$$d of .z, but this seems like an odd way to do what I think you want to do
$$anonymous$$y object orients itself such that it's right side faces the currentPathObj. If that helps
Answer by aldonaletto · Dec 27, 2013 at 11:46 PM
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.
Answer by TheCount · Dec 27, 2013 at 11:56 PM
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!
Your answer
Follow this Question
Related Questions
Move an object toward an angle in 2d space 0 Answers
Problem changing rotation of 3rd Person Controller 1 Answer
Follow x rotation of a target object. 1 Answer
[JS] transform.rotation not working 1 Answer
How to make the player turn left or right only in desired places o edges and not everywhere ???? 0 Answers