- Home /
Issue understanding transform.RotateAround,Questions regarding transform.RotateAround
Hello! I'm new to programming and used transform.RotateAround to create a function within my camera that enables me to rotate it around the player in the y-axis using the Mouse X Axis.
Unfortunately, despite me being able to rotate the camera using the mouse input, the y value gets reset to 0 very quickly, resulting in the camera just jittering in place. I can't locate where my script sets the transform.rotation y value to 0.
Here's my script:
public Transform target;
public Vector3 offset;
public bool useOffsetValues;
public float rotateSpeed = 45.0f;
// Start is called before the first frame update
void Start()
{
//if useOffsetValues is true, use defined values, else...
if (!useOffsetValues)
{
offset = target.position - transform.position;
}
}
// Update is called once per frame
void Update()
{
//Move camera with player
transform.position = target.position - offset;
//Have Camera focus on target
transform.LookAt(target);
//rotate camera
transform.RotateAround(target.position, Vector3.down, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime);
}
Well, you're setting position and rotation in Update just before your RotateAround call. For the position specifically, you're snapping it to the original offset with respect to the target object's world position each frame, so any changes you make with RotateAround will be overwritten.
Since you're only rotating around the "down" vector (0,-1,0), I think you can just preserve that Y offset since your rotation will control your X and Z.
transform.position = new Vector3(
transform.position.x,
(target.position - offset).y,
transform.position.z
);
Your answer
Follow this Question
Related Questions
Resetting transform's original position and rotation after using transform.rotateAround 1 Answer
How to rotate object around another object with less than 1 degree. 0 Answers
Difference between transform.localRotation = Quaternion and transform.RotateAroundLocal 2 Answers
Changing the rotation of an orbiting object 2 Answers
Rotate a vector around a certain point. 3 Answers