Hello, I am very new to Unity (this is my second project(the first contained only force movement)) and I’ve spent around 5 hours making a third person camera myself without watching a tutorial and here’s what I came up with:
using UnityEngine;
public class camera_rotation : MonoBehaviour
{
public Transform cam;
public Transform player;
public float hSpeed = 4f;
public float vSpeed = 4f;
void Update()
{
float h = Input.GetAxis("Mouse X") * hSpeed;
float v = Input.GetAxis("Mouse Y") * vSpeed;
cam.LookAt(player.position);
cam.RotateAround(player.position, Vector3.up, -h * hSpeed);
// if (Mathf.Clamp(cam.eulerAngles.x, 5, 85) == cam.eulerAngles.x)
{
if (Mathf.Clamp(cam.eulerAngles.y, 315, 360) == cam.eulerAngles.y || Mathf.Clamp(cam.eulerAngles.y, 0, 45) == cam.eulerAngles.y || Mathf.Clamp(cam.eulerAngles.y, 135, 225) == cam.eulerAngles.y)
{
cam.RotateAround(player.position, Vector3.right, v);
}
if (Mathf.Clamp(cam.eulerAngles.y, 45, 135) == cam.eulerAngles.y || Mathf.Clamp(cam.eulerAngles.y, 225, 315) == cam.eulerAngles.y)
{
cam.RotateAround(player.position, Vector3.forward, v); // I need to invert this.
}
}
cam.rotation = Quaternion.Euler(cam.rotation.eulerAngles.x, cam.rotation.eulerAngles.y, 0);
}
}
I assume I am misunderstanding something with the vectors or the axis’. I managed to switch the vectors just fine, avoiding weird camera movement. However, everything works but only one thing: the second if statement simply does not want to invert itself. I tried putting minus behind the variable, behind the vector, separating the variables like “float vminus = -Input.GetAxis(“Mouse Y”) * vSpeed;” & “float vplus = Input.GetAxis(“Mouse Y”) * vSpeed;”. I know there is a better, more efficient and easier way for doing that but that is my way of learning and understanding things + it is more fun. So please have a look at the problem I ran into and suggest a fix, rather than coming up with different code.
Yes, I actually refused to check other threads because it would make me rewrite all the stuff probably. I sure would like to hear your tips. I was just wondering where am I wrong with inverting the axis, I have tried:
float v = -Input.GetAxis("Mouse Y") * vSpeed;
//(of course keeping in mind that two minuses (one in the variable equation
and one in the variable calling) creates positive)
cam.RotateAround(player.position, -Vector3.forward, v);
cam.RotateAround(player.position, Vector3.forward, -v);
And it doesn’t seem to be inverting when I test it.
As for my tips, they are something I ingrained when I was messing around/learning to get a nice camera
They’re also kinda standard lol I didn’t adopt them right away.
A 3-part Camera
a ‘Rig’ (empty game object)
child Pivot (empty game object)
child of Pivot : Camera
The rig only rotates on the y axis
the pivot only on the local x axis (and clamped : using a float to alter the value, clamp it then assign with Quaternion.Euler … or to rotate towards if you want some delay/smoothing)
The rig always adjusts to the player’s position. The pivot can have a +y value to its local position (in the start/setup), so it’s a bit of elevation, and the camera has some -z local position value.
You no longer require ‘LookAt’ with this. You can modify the y axis rotation with rotation *= Quaternion.Euler(0, adjustment, 0);
And the x axis, previously mentioned, with the storing of the float.
All I can tell you is that this setup makes me pretty happy. I have some extra modifications for how I like it to behave, etc… but none of which is core to the design. This is also pretty much how the free look camera in the standard assets is setup (at least mainly).
I hope that helps, whether you use it , or in part, or not…
Thank you for that! I will try processing and understanding some of the stuff above in the future. I think I will read some other threads as well before trying to do it myself with the things I learned. Anyway, my camera is not that smooth and the code seems heavy, just the opposite of what you mentioned so I will start over.
Just out of curiosity, were you trying to rotate on the z-axis in your code?
Also, I wish you well in your learning journey & getting a cam that you like
For me, it was very satisfying to get it working as well as I have (might still adjust more in the future). Though it’s a common part of games, it still felt cool to get it on my own/with help, etc…
Thank you! The feeling of something that works after hours of tests… really sweet indeed. I actually have no idea on which axis I try to rotate. I just noticed that when the camera gets in certain X clamp, the Vector3.right turns sideways, so I made an if statement: if the X angle is in that clamp, rotate on the Vector3.forward and outside of that rotate on Vector3.right. Which seems to work, besides that one of the clamp ifs cannot be inverted. Causing the camera rotating in different direction with the same mouse movement between some angles.
Ahh, I see! I think I follow you. You were rotating a “weird” way (for a character) because you were getting … for lack of a better term, rotation “distortion” and trying to compensate.
I hear ya, sympathize… and hope your future work sees less of that lol I went through many similar things…
I’ll let you get back to work, but feel free to write back in the future if things go well… or you have a question, of course lol
Sure thing, friend! Really appreciate the support! Hope we cross our codes in the future (in a working friendly way, of course hahah). Have a good one!