Below is the code that finally uses offset values to fix objection rotation so that the ObjectA looking at ObjectB will retain its initial rotation values i.e before hitting the play button. Its very simple just set the objectA with the script that you want it to lookAt some other objectB and fix the rotation of ObjectA such that it is already looking at ObjectB in the editor and UR GOOD TO GO !!
CAUTION : make sure you change using parameter values and not the Gizmo for predictable results
Note: Rotation with X parameter messes Up the parameters of Y and Z using gizmo which should be a Unity3D glitch. So in your algorithms where you donāt want the X parameter to screw up your other axis rotation during game, note that spherical positioning doesnāt use X parameter to change Object rotation. <= AMAZING
Please PM me or Reply to this thread to give your gratitude. THanks.
var target :Transform;
var initial_y :float; //<= Edited Oct 11,2011
var initial_z :float; // <= Edited Oct 11,2011
private var z;
private var y;
private var x;
//private var initial_x; //<= Edited Oct 11,2011 <= Not sure how changing this will turn out but
//recommended to not use
initial_y =transform.eulerAngles.y;
initial_z =transform.eulerAngles.z;
function Update () {
So, I took this script and put it on a gun, and pointed the gun towards the clowns head⦠(No, really that is what my scene looks like⦠dont ask, I wont tell)
What I got was a gun that pointed 90 degrees from the head. There was some weird thing that happened where as soon as I moved the clown from one area to another that the gun suddenly swapped directions and pointed 90 degreeās the other way.
I am totally confused as to what this script does. (Mind you, I am not an uber math wiz.) And what it does that could counter mind transform.LookAt()
Its hard for me to tell you a fix without having your gun model. But right about now all i can say is play with the rotation parameters. In my model, wen i export it from 3DS Max in 3ds format, my model has the side i am interested facing up. I set Z rotation to 90 degrees and before launchin it is facing the object i want with in a correct manner and wen i play the game it follows so throughout. If your model is fbx, then i think you still might have problems, I havenāt tested it yet with that. If it is then let me know and i might be able to come up with a fix for that. Try to use Y and Z rotations only wen seting your object.
In exterme case, send me your gun and clown models, i am gonna test it out if it still doesnt work out for you in the end.
Damn, Actually i have to say ignore my previous reply cuz since the beginning i was scared that this will pose problems. Ok i have made a fix hopefully should be Final. CHECK my first code. Making variables inital_y and initial_z public, should offer you complete control over setting inital rotation DURING THE GAME.
So to use this effectively, during your game mode play with the settings of initial_y and initial_z until you have it right, and move the target object to see if it works out fine (which it should undoubtly) and then note down the settings of initial_y and initial_z.
Then Enter those noted down settings in Editor mode i.e before launching the game.
The difference between original Unity3D LookAt is that you Canāt or its Incredibly difficult to set initial rotation offset while my algorithm should allow you to manipulate any aspect. Lol and dont worry about having uber math skills, in my next algorithm that again uses spherical system, it took me days to figure out how to perform rotations in spherical system in any direction inspite of learning about this in my course. According to me learning math is different than using math for implementation in code.
I must say this concludes that there lies a bug with unity3D.
Tell me again if you have any problems.