- Home /
help me to rotate an object
Hi everyone. i have to rotate an object around a pvot, now i'm using this and works great Vector3 movePos = new Vector3(0.0f, 1.0f, 0.0f);
float angle = Mathf.Sin(Time.time * Mathf.PI * 0.5f) * 180;
Player.transform.rotation = Quaternion.Euler(angle, 0.0f, 90.0f);
this works perfectly until i increase it until 180, but what i want is something like this
on the left side you can see what my actual code do, what i want is : if i press i key button when player is on the green zone it will able to make a full swing one time, so from -180 it will swing for 360 degrees than will be back on -180, but again when it is in the green zone, so let's suppose it is near 160 degrees, it will continue to swing for 360 degrees otherwise it will put back again on the left side rotation so -180 to 180 until key button will be pressed again on the green zone...
here the point is... how to do that?
i know that it's complicated but i'll really appriciate if someone can help me... thank you in advance
A single quaternion cannot represent a rotation exceeding 180 degrees in any direction. Read more on the Unity $$anonymous$$anual.
Answer by JonPQ · Dec 21, 2018 at 11:06 PM
something like this... just some quick pseudo code...
///setup
bool fullswing = false;
Update()
{
if(inFullSwing )
{
now just do angle rotate here from 0 to 360. adding in startAngle
once finished... set inFullSwing = false;
}
else
{
put existing code here...
if(player presses button && mathf.Abs(angle-targetAngle)<angleThreshhold)
{
startAngle = angle; //store starting place...
inFullSwing = true;
}
}
}
basically i got it, but i'm confused about the count of degrees, so let's suppose that it start from -160 degrees, so i'll rotate it forward to reach the target that is actually -160 to get a full swing, in this case it won't start it's rotation right?
you are startign the full rotation from 'startAngle' which is the angle at time the player hits the button. then from there, you fun a loop for 360 degrees of rotation... say on variable "currentRotation" and during that loop, you set your object's angle to objectAngle = (startRotation + currentRotation); so when it is finished rotating.... your object will be effectively back to startRotation again... at the same point...
if you want to limit your rotation to 0-360 say so it doesn't reach high values like 720... you can use something like (if ang >= 360, ang -=360) and (if ang <0, ang +=360)
Your answer
Follow this Question
Related Questions
issue after 360 degrees rotation 1 Answer
Flip over an object (smooth transition) 3 Answers
calculating looking angle between 2 transforms 0 Answers
Particle System Instantiate's With Original Rotation - C# 1 Answer
Camera not rotating 1 Answer