- Home /
How to make a 3D camera rotate relative to the world?
I followed this tutorial from a few years ago about making an isometric view camera, and usually it works perfectly like the video, but somehow today when I followed the tutorial again the camera does not rotate as expected.
I would like to make it so the camera can rotate relative to the world (So its like the camera is rotating around a pivot and also facing towards it).
Everything else, movement, zoom, whatever works perfectly, just the rotation doesn't, and I've checked many times that nothing should be wrong. Hope someone can explain what the problem is and how to fix it! Thanks!
Here is the rotation parts of the camera controller script:
public float movementTime;
public float rotationAmount;
public Quaternion newRotation;
void Start()
{
newRotation = transform.rotation;
}
void LateUpdate()
{
HandleMovementInput();
}
void HandleMovementInput()
{
if (Input.GetKey(KeyCode.Q))
{
newRotation *= Quaternion.Euler(Vector3.up * rotationAmount);
}
if (Input.GetKey(KeyCode.E))
{
newRotation *= Quaternion.Euler(Vector3.up * -rotationAmount);
}
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * movementTime);
}
}
Answer by MaxieNoob · Feb 16 at 02:15 AM
So, if what you're trying to accomplish is to have a 3rd person camera, sort of like roblox's camera, then its kinda simple:
//CAMERA
if (Input.GetAxis("Mouse ScrollWheel") > 0f & Scroll - .5f > .25f)
{
Scroll = Scroll - .25f;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0f & Scroll + .5f < 5)
{
Scroll = Scroll + .25f;
}
if (Input.GetKey(KeyCode.Mouse1))
{
Camera0.transform.position = RootPart.transform.position + new Vector3(0, 1, 0);
Camera0.transform.rotation = Quaternion.Euler(Camera0.transform.rotation.eulerAngles.x + -Input.GetAxis("Mouse Y") * 3, Camera0.transform.rotation.eulerAngles.y + Input.GetAxis("Mouse X") * 3, Camera0.transform.rotation.eulerAngles.z);
}
Camera0.transform.position = RootPart.transform.position + new Vector3(0, 1, 0);
Camera0.transform.position = Camera0.transform.position + Camera0.transform.forward * -Scroll;
This should work if you do these things: 1. Set the "RootPart" variable as a GameObject that you want it to pivot to 2. Set the "Camera0" variable to your MainCamera that you want to use 3. Add a variable for "Scroll" somewhere in the script
Basically, what this does is it lets you rotate around your "RootPart" and let you use the scroll wheel to scroll, and the Mouse1(right click hold) to rotate the camera @Amberlewis012
@MaxieNoob Thanks for replying, your solution seems to be pretty good, but when i put in the code and everything necessary there were a few weird problems
the camera started shaking wildly after clicking Play, and scrolling to zoom in works, but when i scroll to zoom out, it only zooms out by 0.5 units and then it just stops zooming out...Any solutions?
(edited the zooming out part of this comment)
@Amberlewis012 maybe i didn't clarify enough? Sorry, but i forgot to say, there is a limit to the scroll so it doesn't phase through the character. I just copied it from a script i had, because i thought it'd help. For the camera shaking idk what was wrong with that, it might be because another script is interfering with the camera's position or rotation? Anyway for the zoom thing i recommend removing the "& Scroll - .5 > .25f" part of the if statement.
Update, I found a solution by using RotateAround...and realised my question was probably way too stupid and unnecessary, but oh well. Thanks for replying though!
Your answer
Follow this Question
Related Questions
Jerky 3rd Person Camera Following Movement and Rotation 0 Answers
Screen is 2 cameras with each different properties. 1 Answer
Forward and back movements with a camera emulating an isometric view 1 Answer
How can i make the camera look up and down through keyboard keys? 2 Answers
pixel perfect 2d Sprite in 3d world with isometric camera, how to do it right? 1 Answer