- Home /
LookAt(); is rotating object to the wrong side
Hi there. This is the third time I'm writing this post as first time DNS server crashed when I posted it, resulting in loss of all text, second time I’ve lost electricity… Let’s hope this one goes through!
I'm posting here as last straw to find solution. This means that I have googled and tried many different solutions that work for others. None works for me.
I have problem with LookAt rotations. I'm using sample AI wander script that I've found here :
#pragma strict
var Speed= 10;
var wayPoint : Vector3;
function Start(){
//initialise the target way point
Wander();
}
function Update()
{
transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
if((transform.position - wayPoint).magnitude < 3)
{
// when the distance between us and the target is less than 3
// create a new way point target
Wander();
}
}
function Wander()
{
// does nothing except pick a new destination to go to
wayPoint= Random.insideUnitSphere *5;
wayPoint.y = 0.5;
// don't need to change direction every frame seeing as you walk in a straight line only
transform.LookAt(wayPoint);
Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}
Now, I have an AI model of stick figure that stands on the ground. Once script runs it rotates that gameObject wrong so it looks like character is lying in the air...
Simple solution would be to parent this gameObject, but that's not exactly what I'm looking for because I will have many different AI characters wandering around, each one of them having same/similar/modified script attached.
I googled for hours and tried many solutions but none really helped :
http://docs.unity3d.com/Manual/HOWTO-FixZAxisIsUp.html http://answers.unity3d.com/questions/357058/rotation-is-just-wrong.html http://answers.unity3d.com/questions/54973/rotate-an-object-to-look-at-another-object-on-one.html http://answers.unity3d.com/questions/36255/lookat-to-only-rotate-on-y-axis-how.html
I tried to adjust all codes found on those links but I couldn’t make it work. (Tried Quaternion as well) No matter what I did I would end up with that weird rotation.
I’m also having this problem in my “Click to move” script, that I picked up from web and modified. But I handled that with parent, tho for sure I would like to fix it properly. But nevermind that, If we manage to find solution for this issue I might find other one myself :) Used this script just for an example.
Thanks !
can you send unity package of this thing working on your side? because i can't figure out your exact situation and problem.
I'm with @Patel Sagar on this one. It is unclear what is the root of your problem. I'm guess it is one of two issues (or perhaps both):
You built the model wrong. Unity expects that your 3D models be constructed so that the 'forward' side is facing positive 'z' and the 'up' side is facing positive 'y' when the rotation is (0,0,0). This is the basis for much movement code and rotation code including Transform.LookAt() and Quaternion.LookRotation(). Your movement code indicates that this is likely not the problem.
You are not defining the preferred axis of rotation for the LookAt() function. LookAt() function takes an optional second parameter that defines the preferred axis of rotation. The default axis is 'y', and works well for characters/objects that are moving around on the XZ plane.
You seem to be constructing your wayPoint as an arbitrary point in 3D space. Since there are an infinite number of ways that 'forward' can be pointed at, you'll need to define for us the criteria for how you want the object to behave when you exectue LookAt().
Hi! Thanks for replies and sorry for replying late. I managed to include everything (except for textures) into package and I'm sending you all models and scripts that make issues. https://dl.dropboxusercontent.com/u/38828522/Test.unitypackage
Sorry for missing out textures.
I intentionally put camera my project uses as it could be problem as well. When you click on terrain you will see the "Convoy" gameObject moves and rotate wrongly. That's what I fixed by parenting it. You can also see a stick model with AI wander script, it also rotates wrongly.
I did make mistake when I exported model, robertbu, but I fixed it after and reimported proper one, still having issues.
As for the second parameter, I sadly didn't check documentation so I learned just now about it. Didn't test it in my project though!
Expected behavior would be for stickman to stay in "standing" position, as I planned to replace it later with an human model that walks.
Thanks!
Answer by Bunny83 · Jul 14, 2014 at 07:57 PM
You said you "fixed the rotation", but you didn't:
As you can see the blue arrow, which is your forward direction points upwards. When you do a LookAt, Unity will point the blue arrow towards your target and the green arrow upwards.
You have to create a new empty gameobject, attach your AI script there and make your model a child of the new gameobject. This new gameobject will be your actual object which moves around with the blue axis pointing forward and the green one upwards.
Here you can see the new Gameobject i created. I called it Walker. Here you have to attach your AI script. This will be the object which moves around:
Here you see your model as child object. This object don't have the AI script attached. It's just the model of the walker. Note how the child object is rotated inside the parent. Now the parent's forward axis (the blue one) points actually forward. I also put the new parent at the bottom as it will simplify movement. You just need to move your child object upwards so the feet hit the ground:
Hey, thanks for reply! Yes I'm aware that I can parent it and fix it this way, but only question that is left now is - How will parenting many "Walkers" affect the game? Should I worry ? I will probably have more than 20 of those walkers on scene and I would need to parent each one of them.
I did export my model from blender exactly how you said so :
positive 'z' and the 'up' side is facing positive 'y' when the rotation is (0,0,0)
And thus I thought that that fixed it!
But thank you very much about the info, It helped me. I will try to fix the rotations within blender again and see what happens ! In the meantime, I think that part of your answer is an actual answer because the problem is obviously wrong orientation that was imported that way !
Edit :
Sorry for double post. If I understood you correctly, this should be proper way of axes ? Y is up, Z is on the side.
It's the same like the empty Game Object as it appears, but model still rotates just like it did before, It doesn't use Z as forward but rather Y. I would like to find a way to fix this from happening completely so that I don't have to parent every object.
Thanks!
EDIT 2
Even tho I exported model with proper axes that you supplied, I didn't bake them into it, which was the problem. Now everything is fixed. Thanks for great info, patience and support ! $$anonymous$$arked as answer!