- Home /
rotate gameobject
i have 2 gameobject "TanHead" and plane(with position increment in x ) ,what i want is my TanHead to follow plane with only rotation ,position of Tank is fixed .so when my gameobject (plane) is above Tank the rotation on TanHead will be 270 degree. i have looked and tried lookat function but i cant use that because i want to fix tank rotation upto 0 to 90 degree
here is my my image
here is what it should look like when my plane goes by
now what m using for rotation
var target : Transform;
var myself:Transform;
var tempAngle:float;
private var angle: Vector3 = new Vector3(360,270,0);
@script AddComponentMenu("Camera-Control/Smooth Look At")
function LateUpdate()
{
myself.LookAt(target);
if(myself.transform.rotation.x != 0)
//seem to get a 0 if my target is in
//certain places because I am using a mouse pos and hit with plane.
{
tempAngle = myself.transform.rotation.x * 100; //don't know how to read a euler
//angle into a float so I just multiplied by 100 to get the angle.
}
else
{
tempAngle = tempAngle;
}
myself.rotation = Quaternion.Euler(tempAngle,270,0);
}
what i can do to fix it because with this script when my plane goes to 270 degree it direct it self to 360 degree causing big jerk thank you
It depends on what you want it to happen after this angle. Does it stop rotating at all or does it flip to the other side?
Well just don't do the call to LookAt() function if headTank.transform.localRotation.eulerAngles.x > 90.
diegzumillo i want it to go back to its previous position after it reaches its limit with smooth rotation
Is the angle can't setting by you want? $$anonymous$$aybe you can see following Quesstion that Proposed by me.link text
Answer by Nodgez · Mar 26, 2014 at 02:49 PM
void Aim()
{
Vector3 direction = Vector3.Normalize(targetTransform.position - transform.position);
float angle = Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle,Vector3.forward);
transform.localRotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime);
}
This is what I think you want. Attach it to the TanHead. You might need to change the Pivot point of your sprite to get the effect right. you find the direction you need to look and then use the Atan2 function to rotate to it in 2 Dimensions.
If you want the TanHead to follow the gameobject precisely then remove the Quaterion.Slerp() function and just set it to the rotation variable. you might also want to use Mathf.Clamp() on the angle but that is up to you.
Your answer
Follow this Question
Related Questions
LookAt Limit Up and Down rotation only 2 Answers
Camera following/looking at aircraft 1 Answer
Add rotation to LookAt (CarSmoothFollow) ? 1 Answer
Bones [or something] to 'follow' or 'chase' each other! 1 Answer
how to limit rotation 2 Answers