changing camera position through c# script

hi

i am manipulating the main camera position through a c# script
i want to change the camera position and rotation through basic keys like up, down, etc
that i manage to do / achieve, but the outcome is slightly not what i desire

i use:
Vector3 old_position, new_position;
new_position.R = old_position.R ± delta;
Camera.main.transform.position = Vector3.MoveTowards(new_position, old_position, speed);

in the above, R is one of the axes (x, y, or z)
i change one of the axis values at a time, and only 1 axis value at a time, in accordance to the corresponding key pressed.

this works, but the outcome is that all 3 axes’ values end up changing, not just 1 axis value, as supposed.
the primary axis reflects the predominant change, and the other 2 axes change with it, yet to a lesser degree
for instance, if i change the camera x axis value, because the key for the x axis was pressed, the camera x axis value do indeed change significantly, yet the z and y axis values change too, with it, albeit to a lesser extent

e.g. camera_position before {1, 0, 0} >> camera_position after {3, 0.5, 0.5} with a key press to move the x axis

i take it that this is due to interpolation, in turn to using something like Vector3.MoveTowards()

so i simply switched to:
new_position.R = old_position.R ± delta;
Camera.main.transform.position = new_position;
old_position = new_position;

this solves the issue and it works fine for camera position, but not camera rotation.

if i use such an approach with camera rotation, all 3 axes’ values change together, albeit the primary selected axis changes more predominantly.
can anyone tell me why do all 3 axes change together, when i manipulate a single axis alone?

thanks

Stop “programming” the camera. You’ll waste a lot of time implementing a broken solution. Use Cinemachine. Manual is a quick read, easy to understand, and trivial to set up. Unity’s free “Starter Assets” on the store provide a first/third person camera setup.

1 Like

ok thanks, will consider.

still, can you tell me why the above occurs?

Programming the camera is a good introduction to scripting and orientation in space, but as @CodeSmile mentioned, Cinemachine does an excellent job straight out of the box. If you’re a beginner, practice your skills with scripting, but if you prioritize polished quality (without reinventing the wheel), then Cinemachine is the way to go.

To your question:

nice. thanks

I second (or third) the Cinemachine recommendation. They even have their own froup:

If you absolutely insist on making your own camera controller, the simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.

private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.

Don’t use cinemachine for anything other than quick prototypes.

If you need to get something working for you to move to other things, fine, but don’t get in too deep and plan to replace it with something else after you know what you want out of your camera (and have a bit more experience?).

Can you show the actual code? Without seeing it the best I can do is guess that it has something to do with eulers vs quaternions. Unity is internally representing rotations with quaternions. If you modify the eulers and then pass it through to Unity it can result in multiple axis being changed for the quaternion.

i come from a low level background so i am naturally inclined to want to do things low level
and i dont know all the libraries and methods yet

seems i was indirectly invoking transform.lookat via transform.rotation. if i consider something like transform.lookat, it helps me understand what possibly happened as i was manipulating the camera rotation

also nice explanation thanks dear