How to use LookAt for only one axis?
I want to rotate my target(gameobject) towards my tank(gameobject) and for that i am using LookAt. my target is a simple gameobject which has a barrier(rectangular line) which comes between tank and target so the target can't be shooted directly. So, whenever the target changes its position, the barrier should also change the position and set between target and tank. according to Image No. 1. But when i use below code what happens is instead of changing only Z axis rotation, it also changes X&Y axis and gets screwed up like shown in Image No2. Now how to rotate target in only Z axis ?
my code is:
Vector3 lp = Tank.transform.position; //Tank position to look at
transform.LookAt(lp); // LookAt used
Answer by cjdev · Feb 03, 2016 at 12:56 PM
Transform.LookAt actually has two parameters and one of them has a default which is why you only need to put in one to make it work. The first parameter is obviously what to look at and the second parameter is the "up" axis of rotation. By default that is Vector3.up, the positive y-axis, which is why your tank is rotating as if it was being twisted around the y axis. You can change this second parameter to your "up" direction and you should get the results you're looking for.
Thanks for looking over and ya, this link helped me
Your answer
Follow this Question
Related Questions
Rotating an Object (To Face Another Object) Only on X and Y Axis 3 Answers
look at local time delay 0 Answers
What is the difference between setting transform.eulerAngles and transform.rotation? 1 Answer
rotate Y axis towards position 1 Answer
How do I orient one axis of an object following LookAt/LookRotation? 0 Answers