- Home /
The question is answered, right answer was accepted
LookRotation freaks out..
hello everyone, I have a tank which has 3 moving parts. First one is the tank itself, which shall look to target and it does perfectly. Second one is the turret which shall look to target and it does perfectly. Third one is the barrel and damn barrel suppose to look to target but it does not. tank and turret rotates on Y Axis, barrel suppose to rotate on X axis. I tried several ways but all failed. tried to clamp rotation of other axises but failed. tried to use turrets Y and Z axis because barrel is child of the turret and failed again. it is so weird that same code doesnt work for pitch rotate. let me show you what i tried already..
var lookPos = Target.transform.position - barrel.transform.position;
lookPos.x = 0;
var rotation = Quaternion.LookRotation(lookPos);
barrel.transform.rotation = Quaternion.Slerp(barrrel.transform.rotation, rotation, Time.deltaTime * RotationSpeed);
Vector3 targetPostition = new Vector3(barrel.transform.position.x, Target.transform.position.y, Target.transform.position.z);
barrel.transform.LookAt(targetPostition);
both methods above failed. Turret is child of tank and barrel is child of turret. The most successfull aproach was
barrel.transform.LookAt(Target.transform.position);
which is useless for me because it moves faster than turret and i dont want barrel to move around by itself. What i want? I want tank and turret to rotate to target on Y axis only and barrel to rotate on X axis only in individiual speeds. Thank you for your time..
edit : second code removed target.x and added barrel.x
Edit 2 : for those who need correct answer is given in the link below. tagging subject as answered. http://answers.unity3d.com/questions/388185/make-the-turret-automatically-rotate-to-look-at-wh.html
Answer by Commoble · Apr 22, 2017 at 01:14 AM
The first thing I see that you're doing wrong is that you're using Slerp wrong.
Slerp (and other Lerp-like functions) take in three values: A, B, and T, where A and B are anything and T is in the range [0,1]. It returns a value between A and B based on T: if T is 0, then it returns A. If T is 1, then it returns B. If T is between 0 and 1, then it returns a corresponding value between A and B.
Calling Time.deltaTime returns the amount of time (in seconds) the previous frame lasted; if you don't have any performance problems, it's typically roughly 1/60th of a second (around 0.0167F).
Multiplying Time.deltaTime by your speed variable and using the result as your T-value in Lerp or Slerp is virtually never useful. The usual way to use Lerp and Slerp is to gradually change your T-value from 0 to 1 over the course of many Update events (or in a coroutine) so that your rotation or position starts at one value at one time and ends at another value at another time.
Answer by omerselman · Apr 23, 2017 at 11:40 AM
http://answers.unity3d.com/questions/36255/lookat-to-only-rotate-on-y-axis-how.html those codes are actually working well for Y axis but not for X axis...