Trying to make an all round view camera by using the mouse right click button. Some what similar to the unity editors view. I am having problems with the rotation about vectors parallel to the xz plane however. My character stands parallel to the Y axis. so I tried to get a Vector that would always be parallel to the xz plane yet perpendicular to the direction the camera is facing. However I seem to be getting a vector pointing somewhere off to the left corner of my screen (camera is facing player from the back and slightly angled downward). Can anyone explain what I am doing wrong? Note: I commented out the rotation about the Y-axis just for testing purposes.
//rotate camera from mouse input
if (Input.GetMouseButton(1))
{
float X = Input.GetAxis("Mouse X");
float Y = Input.GetAxis("Mouse Y");
/*
//adjust y rotation
if (X > 0)
{
transform.RotateAround(transform.parent.localPosition, Vector3.up, -100f * Time.deltaTime);
}
else if (X < 0)
{
transform.RotateAround(transform.parent.localPosition, Vector3.up, 100f * Time.deltaTime);
}
*/
//adjust x-z rotation
if (Y > 0)
{
Vector3 rotAround = Vector3.Cross(transform.forward, Vector3.up);
transform.rotation = transform.rotation * Quaternion.AngleAxis(100f * Time.deltaTime, rotAround);
}
else if (Y < 0)
{
Vector3 rotAround = Vector3.Cross(transform.forward, Vector3.up);
transform.rotation = transform.rotation * Quaternion.AngleAxis(-100f * Time.deltaTime, rotAround);
}
}
You should use Vector3.left instead of “rotAround”.
When you multiply quaternions, the second quaternion will apply the rotation considering the left quaternion as “origin”, so it’ll basically rotate your character on its local X axis no matter its Y axis rotation.
What you’re doing here is that you get the perpendicular vector of the plane designed by Vector3.up AND the LOCAL forward vector of your transform. Meaning that this wont be Vector3.left but something already relative to the Y rotation of your character.
So if you then mult rotAround considering your character’s origin, it won’t rotate it to the relative X axis of your character, but on something else.
If I am understanding your goal correctly (rotate your camera around the Y axis while Right Click is pressed), then why not do something a LOT simpler (js):
var MainCamera : GameObject;
var Sensitivity = 1.0;
function Update ()
{
if (Input.GetMouseButton(1))
{
MainCamera.transform.rotation.eulerAngles.y += (Input.GetAxis("Mouse X") * Sensitivity);
}
}
Then play around with the sensitivity a little to get it right, and like this you can also offer the camera sensitivity as an option in the Menu.
EDIT: And you can do the same for the X axis, if you wanna give an up/down control too, only use “Mouse Y” axis for that.
Thanks @FairGamesProductions and @Ijatsu
Both of your methods are valid and work. However I found @FairGamesProductions 's method to be a bit tedious in C#. I have to assign temporary variables and that sort of thing in C# as opposed to Java (unless that was just pseudo code, sorry I haven’t used java in ages).
I managed to find another method specifically for rotating about the x axis. However its not so much of a rotation but more of a translation along the vertical axis and having the camera to look at the player (which is essentially the rotation part). It seems a little unconventional but it gave better results for MY game. It may not work for everyone. Anyway here it is: