- Home /
Rotate camera around object with camera follow
Hi! I have a cube that moves, i have a camera follow script that follows that cube. That is.
public class CameraFollow : MonoBehaviour { public Transform target;
public float smoothSpeed = 0.125f;
public float rotationAngleY = 90f;
public float rotationSpeed = 10f;
public Vector3 offset;
private void FixedUpdate()
{
Vector3 desiredPosition = target.position + offset;
transform.position = desiredPosition;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition + offset;
}
I need to rotate camera around this cube by pressing button on 90 degrees, i did that, but camera rotates around itself, not a target. But when i comment the cameraFollow part of script, it works correctly
private float targetAngle = 0;
const float rotationAmount = 1.5f;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
targetAngle += 90.0f;
}
if (targetAngle != 0)
{
Rotate();
}
}
private void Rotate()
{
if (targetAngle > 0)
{
transform.RotateAround(target.position, Vector3.up, -rotationAmount);
targetAngle -= rotationAmount;
}
}
Will be pleausure for any help, stuck on this for a day.
Answer by jawad111ahmad · Apr 03 at 10:37 AM
create a new empty gameobject and put it inside your cube then make the camera as child of this gameobject now apply the camera scrip on this empty gameobject and remove the camera script from camera and now rotate that gameobject it will rotate the camera around the cube
Your answer

Follow this Question
Related Questions
How to change default FPS controller to rotate when mouse is at end of screen 0 Answers
How to rotate the camera with the player smoothly without the camera snapping back and forth? 2 Answers
Camera won't follow character 2 Answers
how to make my bones follow the camera(FPS) 0 Answers
Camera rotation 0 Answers