0

I'm trying to make the camera rotate around an object according to the movement of the mouse, and this is what I've come up with:

void Start() { direction = new Vector3(1f,0f,0); direction.Normalize(); Debug.Log(direction); mousePosCurr = Input.mousePosition; } void Update() { mousePosPrev = mousePosCurr; mousePosCurr = Input.mousePosition; mouseVector = (mousePosCurr - mousePosPrev); mouseVector = mouseVector * sensitivity; direction = Quaternion.Euler(mouseVector.y, mouseVector.x, 0) * direction; direction.Normalize(); Debug.Log(direction); this.transform.position = (target.position + tOffset + (direction * distance)); transform.LookAt(target.position + tOffset, Vector3.up); } 

Now, I'm sure that there are at least a million problems with this code, however the main one I'm facing right now is that the x rotation does not work correctly, unless the y rotation is in a specific direction.

What's happening is basically that the more I rotate in the y axis the more "inprecise" the x movement becomes. So for example, when the camera is south of the object I'm rotating it around, the x rotation works exactly as it should, you move the mouse up, and the camera points up and viceversa, but when it's west of it the movement of the mouse required for the vertical rotation is no longer vertical, but diagonal, and when it's north of the object the movement is inverted: moving the mouse up makes the camera point down and viceversa.

This seems most likely caused by the fact that regardless of the horizontal rotation of the direction vector, I'm still rotating along the x axis in the same direction, as if the camera was south of the object.

However, even having (hopefully) identified the problem, I'm still clueless as to how to solve it, so I'm asking for help here.

Note: I just noticed that I probably used the term "rotation" wrong in this post. When I write "rotation of the camera", I'm actually referring to the rotation of the "direction" vector.

1 Answer 1

0

alright so I am not the best in Unity so this might be completely useless but when I try to rotate my camera around an object I create an empty game Object and make the camera a child of it, like this

so that I can just just rotate the Game Object to rotate the camera around a fixed point maybe your code will work

Sign up to request clarification or add additional context in comments.

1 Comment

I already tried (thought I'm not 100% sure I did it right), how do you usually get the amount by wich you need to rotate the object? Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.