Search Unity

What did I do here...(vector3/transform/position question)

Discussion in 'Getting Started' started by Altissimus, May 14, 2019.

  1. Altissimus

    Altissimus

    Joined:
    May 11, 2015
    Posts:
    49
    I was trying to move my camera around using:

    Code (CSharp):
    1. public Transform theCamera;  // main camera
    2. private Transform theCameraStore; // variable storing the original position of the main camera, to return to later
    3. public Transform charSwitchHolder; // destination of the y axis (x and z to remain unchanged)
    4. private Vector3 camTargetPosition; // for Lerping to

    So on the way out,

    Code (CSharp):
    1. camTargetPosition = new Vector3(theCamera.position.x, charSwitchHolder.position.y+.3f, theCamera.position.y);
    ...sets the target, and:

    Code (CSharp):
    1.  
    2. //update
    3. theCamera.position = Vector3.Lerp(theCamera.position, camTargetPosition, cameraSpeed * Time.deltaTime);
    ...runs in update to constantly check the camera position and move accordingly. (The above all works fine.)

    Then I wanted to move it back again. I used theCameraStore to record the camera's original position:

    Code (CSharp):
    1.  
    2. // Start
    3. theCameraStore = theCamera; // to store the camera's position

    ...and changed the target as follows:

    Code (CSharp):
    1.       camTargetPosition = new Vector3(theCameraStore.position.x, theCameraStore.position.y, theCameraStore.position.z);
    All looks perfectly logical, right? Well, nothing happened.

    An hour of trying to work out why not later, I changed:

    Code (CSharp):
    1. private Transform theCameraStore; // variable storing the original position of the main camera, to return to later
    2.  
    3. // to
    4.  
    5. private Vector3 theCameraStore; // variable storing the original position of the main camera, to return to later
    and

    Code (CSharp):
    1. //in start
    2. theCameraStore = theCamera;
    3.  
    4. // to
    5.  
    6. theCameraStore = new Vector3(theCamera.position.x, theCamera.position.y, theCamera.position.z);
    and called it with

    Code (CSharp):
    1.  camTargetPosition = new Vector3(theCameraStore.x, theCameraStore.y, theCameraStore.z);
    ...and this worked exactly as I wanted it to.

    But wow, my mind is blown with all this transform/vector3/position stuff.

    I think I get that theCameraStore is a single point (so a Vector3), but I don't get why the original Transform didn't achieve the same goal?

    So tell me, please...

    Why doesn't what I did first work? Moreover, what did I actually tell it to do first time out?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It would be a lot easier to follow if you just posted the script all together instead of breaking it up with your commentary. But...

    Code (CSharp):
    1. camTargetPosition = new Vector3(theCamera.position.x, charSwitchHolder.position.y+.3f, theCamera.position.y);
    Did you really mean to set theCamera.position.y as camTargetPosition.z?


    Well that isn't what you did though. You stored a reference to the transform component, not the value of the theCamera.position vector3. theCameraStore.position will always equal theCamera.position since these are both just references to the exact same transform component.