- Home /
How to rotate camera around an object to a certain amount of degrees?
I tried to rotate the camera around its own axis when i press a key by using RotateAround but the problem is that it instantly teleports the camera at the given angle instead of smoothly rotating to it.I tried multiplying the angle by time.deltatime but it just moved a little when i pressed the key.
if (Input.GetMouseButtonDown(1))
{
transform.RotateAround(parent.position, Vector3.up, angle);
}
Answer by Hellium · Mar 15, 2020 at 01:31 PM
While the solution of metalted will effectively rotate smoothly the object, it requires you to hold the mouse button down.
Here is a solution to automatically rotate object only when the mouse button is pressed down.
using UnityEngine;
public class Example : MonoBehaviour
{
public float rotationSpeed = 10; // = 10° / sec, adjust value in inspector
public float angle = 30; // adjust value in inspector
public Transform parent;
private float remaininAngle;
void Update()
{
if(Input.GetMouseButtonDown(1))
remaininAngle += angle;
float newRemainingAngle = Mathf.MoveTowards(remaininAngle, 0, rotationSpeed * Time.deltaTime);
float delta = remaininAngle - newRemainingAngle;
remaininAngle = newRemainingAngle;
transform.RotateAround(parent.position, Vector3.up, delta);
}
}
Note that if you click again while the rotation is running, it will rotate further. If you want to prevent this behaviour. Do as follow:
if(Mathf.Approximately(remaininAngle, 0) && Input.GetMouseButtonDown(1))
remaininAngle += angle;
Thank you so much this is exactly what i was expecting ^^;
Answer by metalted · Mar 15, 2020 at 01:14 PM
The example script on this page : https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html, has a nice example script that we can change to suit your needs:
using UnityEngine;
//Attach this script to a GameObject to rotate around the target position.
public class Example : MonoBehaviour
{
private Vector3 target = new Vector3(5.0f, 0.0f, 0.0f);
void Update()
{
// Spin the object around the world origin at 20 degrees/second.
transform.RotateAround(target, Vector3.up, 30 * Time.deltaTime);
}
}
So as you see in this script, using Time.deltaTime is the right way to go. The problem in your code is that you are using the wrong input type.
Input.GetMouseButtonDown will be true only on the moment the mouse button is pressed, so only one frame. The object will only rotate for that frame. If you want to get continuous input from the mouse button you should use Input.GetMouseButton(). So the changed script will look like this:
using UnityEngine;
//Attach this script to a GameObject to rotate around the target position.
public class Example : MonoBehaviour
{
private Vector3 target = new Vector3(5.0f, 0.0f, 0.0f);
void Update()
{
if(Input.GetMouseButton(0)){
// Spin the object around the world origin at 20 degrees/second.
transform.RotateAround(target, Vector3.up, 30 * Time.deltaTime);
}
}
}
Your answer
Follow this Question
Related Questions
Third person Camera stop it from going lower than the terrian 0 Answers
Would anyone teach me how to create camera movement and look controls? 2 Answers
Cinemachine input axis camera movement,cinemachine input axis control with script 1 Answer
I only rotated X, but Y and Z are shifting as well when I play the game. 1 Answer
How to allow camera to complete an upside down rotation while using LookAt() 0 Answers