- Home /
rotateAround pivots at unexpected point
My intention is to have the camera orbit the player, however it spins in place instead of moving around the player at a fixed distance. Have I misunderstood something? targetTransform is the player transform;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowTarget : MonoBehaviour
{
public Transform targetTransform;
public float cameraOffset;
[Range(0.01f, 1)]
public float smoothFactor = 0.5f;
void Start()
{
transform.position = GetCameraOffset();
transform.LookAt(targetTransform);
}
void LateUpdate()
{
// Follow Player
transform.position = Vector3.Slerp(transform.position, GetCameraOffset(), smoothFactor);
// Rotate around Player if mouse button is held down
if (Input.GetMouseButton(0))
{
transform.RotateAround(targetTransform.position, Vector3.up, Input.GetAxis("Mouse X") * Time.deltaTime * -180);
transform.RotateAround(targetTransform.position, transform.right, Input.GetAxis("Mouse Y") * Time.deltaTime * 180);
return;
}
}
Vector3 GetCameraOffset()
{
return new Vector3(cameraOffset, 0, 0) + targetTransform.position;
}
}
Answer by CardboardComputers · Aug 13, 2020 at 10:55 AM
You're setting the transform.position
at a fixed offset (or rather, you're lerping it to that fixed offset, specifically on the line following // Follow Player
) each frame, so no matter where RotateAround
wants to put your camera, it'll inevitably be returned to the original offset the next frame. You'll have to find a different way to tell the camera to follow the player if you want it to be able to orbit the player, otherwise you'll always be stuck around new Vector3(cameraOffset, 0, 0) + targetTransform.position
.
Thanks, of course it was that haha. I thought I had tried commenting it out at some point but I guess I had not.
Your answer
Follow this Question
Related Questions
How do I set a child object to not rotate if the parent WILL be rotating? 1 Answer
An alternative to RotateAround for collisions 0 Answers
Issue with transform rotations and local rotations 1 Answer
Get rotation based on closest 90 degrees to camera transform? 2 Answers
how to rotate and transform an Game object using same button in unity ? 0 Answers