- Home /
Rotation of an Object yielding no results
Hello.
I have 3 copies of the same model. One has a default X rotation of 90, one has a default Y rotation of 90, and one has a default Z rotation of 90.
When instantiated in game, I default the rotation to the instantiated position's rotation which looks like:
GameObject test = Instantiate(testObject, transform.position, transform.rotation) as GameObject;
As expected it spawns on the right point, and also as expected, it's facing upwards.
Then I apply a one-shot rotation to it with a simple Transform.Rotate.
vector3 _spellDirection = viewDirection;
test.transform.Rotate(viewDirection, 270);
I've tried every number possible as the second parameter, and even without a second parameter. It doesn't rotate. I've even tried it as part of a While() statement, and it fires, though doesn't move at all.
What could possibly be the issue?
My goal is to get it to face towards a certain angle, but it won't do anything but stand straight up on all instances of the models that I imported.
Thanks for any help.. Probably a pretty simple answer, though I can't get it to move at all.
Answer by Eric5h5 · Dec 21, 2012 at 05:04 AM
Your Rotate code does work; you have some other issue. Try trouble-shooting, and just use this:
transform.Rotate (Vector3.up, 90);
Put that on a cube or something and it will rotate 90 degrees. However it would be more common to do:
transform.Rotate (Vector3.up * 90);
From your description, it sounds like maybe you should use Transform.LookAt though.
I've AL$$anonymous$$OST got it working with using Transform.LookAt.
The only issue that I'm encountering is that I'm not sure what to use for the transform target parameter. Whenever I use a transform.forward of the "casting object," it seems to be a little off.
On a plus side it's now facing the general direction, just off by a few degrees because of not understanding the transform target parameter.
my code:
test.Transform.LookAt(cCamera.hero.transform.forward , spellDirection);
I still don't know the root cause of why my rotation is off on my model (after importing 3 different variations of the same model) but for now it's working with a little discrepancy in the angles.
Eventually I'll figure it out completely but for now it'll be good, as it's only meant for a basic test of the system.
Thanks for the help.
Oh, and also, I'm using Transform.LookAt, and the cause for the slight flaw is the transform target parameter. After putting in like 200 different parameters, it still remains off.
Idk how you have the axes set up, or if this will help, but try parenting your model to an empty object, and attach your code to the new parent ins$$anonymous$$d of the model itself.
AS stated earlier, it was a noob issue. What was happening was I was exporting the model without applying the rotation and scale. The final code that's working is..
transform.LookAt(Camera.main.transform);
I applied scale on the 2 models that weren't facing the Z axis, and of course on the one that was I just exported it without thinking twice.
Thanks for the help guys; now I know about Transform.LookAt
Your answer
