I'm trying to make the camera rotate around the player with the movement of the mouse on the Y axis as it follows the player. To do this I am using the following code:
private Vector3 offset;
public Transform target;
private void Start()
offset = target.position - transform.position;
private void LateUpdate()
target.RotateAround(transform.position, Vector3.left, -Input.GetAxis("Mouse Y"));
target.position = transform.position + offset;
If I remove the command to follow the player, the camera rotates around it, but if the command is enabled it rotates around itself, not the player. How to fix it?
Answer by corpsinheretoo
Mar 19 at 07:10 PM
In the first line of LateUpdate you call RotateAround - which changes both the rotation and position of target. In the 2nd line you reset the position - effectively undoing that part of RotateAround - so all you have is the rotation. Try making your camera a child of the player in the hierarchy. Then it will automatically follow the player.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
4 People are following this question.
Weird Results When Changing Transform And Rotation
RotateAround object around player when pushing button.
DirectoryInfo().GetFiles works incorrectly in Standalone
Unity4 Ignoring Inspector/Initialized Public Value in Build
No shadows in the game, but there are in the editor.