- Home /
Smoothly Rotate the Object
Hi there. I'm trying to rotate my camera slowly. The first attempt is succesful. (from 0,0,0 to 0,90,0) but the others just failure.
Can you help me?
void FixedUpdate()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.transform.tag == "SagTus")
{
angle += 90;
Debug.Log(angle.ToString());
sag=true;
}
}
if(Physics.Raycast(ray, out hit))
{
if(hit.transform.tag == "SolTus")
{
angle -= 90;
this.transform.rotation = Quaternion.Lerp(Quaternion.Euler(new Vector3(0,0,0)),Quaternion.Euler(new Vector3(0,angle,0)),Time.time);
}
}
}//end input if
if(sag)
{
this.transform.rotation = Quaternion.Slerp(Quaternion.Euler(new Vector3(0,angle-90f,0)),Quaternion.Euler(new Vector3(0,angle,0)),Time.time * speed);
if(this.transform.rotation == Quaternion.Euler(new Vector3(0,angle,0)))
{
sag = false;
}
}
}
Comment
You can't use FixedUpdate for that, because it doesn't run every frame, which means you will sometimes miss the GetButtonDown call (depending on how fast Update is running). Only use FixedUpdate for physics.
Your answer
Follow this Question
Related Questions
Raycasting, rotating a ray 360 degrees 2 Answers
Image Rotate Gallery Snap 0 Answers
make rotation smooth and instant 2 Answers
How do I call a function containing a while loop, inside the update function? 2 Answers
Doesn't Add Rotation Over 180 2 Answers