Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question changing camera position through c# script

Discussion in 'Scripting' started by barbrian1, Feb 9, 2024.

  1. barbrian1

    barbrian1

    Joined:
    Jan 24, 2024
    Posts:
    19
    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
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,797
    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.
     
    Kurt-Dekker likes this.
  3. barbrian1

    barbrian1

    Joined:
    Jan 24, 2024
    Posts:
    19
    ok thanks, will consider.

    still, can you tell me why the above occurs?
     
  4. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    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:
     
  5. barbrian1

    barbrian1

    Joined:
    Jan 24, 2024
    Posts:
    19
    nice. thanks
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,656
    I second (or third) the Cinemachine recommendation. They even have their own froup:

    https://forum.unity.com/forums/cinemachine.136/

    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.

    Code (csharp):
    1. private Vector3 WhereMyCameraIsLocated;
    2. private Vector3 WhatMyCameraIsLookingAt;
    3.  
    4. void LateUpdate()
    5. {
    6.   cam.transform.position = WhereMyCameraIsLocated;
    7.   cam.transform.LookAt( WhatMyCameraIsLookingAt);
    8. }
    Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,707
    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?).
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,111
    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.
     
  9. barbrian1

    barbrian1

    Joined:
    Jan 24, 2024
    Posts:
    19
    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
     
  10. barbrian1

    barbrian1

    Joined:
    Jan 24, 2024
    Posts:
    19
    also nice explanation thanks dear