Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

transform.position = _target sets the object in a place other than _target

Discussion in 'Physics' started by Kacper1263, Apr 23, 2019.

  1. Kacper1263

    Kacper1263

    Joined:
    Apr 7, 2019
    Posts:
    11
    Hi.
    For several hours I have been trying to move the object on the scene. At the beginning I used Vector3.MoveTowards because I need smooth motion but it did not work. For the test I gave
    camera.transform.position = destinationCameraPos;
    but it turns out that it does not work either. Although target (vector3) is set to -2.1, -6 my camera goes to positions 264,486,137.

    After pressing the button on the UI, I want the camera to be rotated and shifted under a different canvas. Camera is a child of another object.

    Here's my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraMove : MonoBehaviour
    6. {
    7.     public Camera camera;
    8.     public Animator animator;
    9.  
    10.     [Space]
    11.     [Header("Rotation to look at credits")]
    12.     public float targetCreditsRotX;
    13.     public float targetCreditsRotY;
    14.     public float targetCreditsRotZ;
    15.  
    16.     [Space]
    17.     [Header("Position to look at credits")]
    18.     public float targetCreditsPosX;
    19.     public float targetCreditsPosY;
    20.     public float targetCreditsPosZ;
    21.  
    22.     private Quaternion destinationCredits;
    23.     private Quaternion destinationMain;
    24.     private Quaternion startRotationDown;
    25.  
    26.     private Vector3 destinationCreditsPos = Vector3.zero;
    27.     private Vector3 destinationMainPos;
    28.  
    29.     private bool toCredits;
    30.     private bool toMain;
    31.  
    32.     void Start()
    33.     {
    34.         //startRotationDown = Quaternion.Euler(90F,0,0);
    35.  
    36.         animator.enabled = true;
    37.  
    38.         destinationMain = Quaternion.Euler(0, 0, 0);
    39.         destinationMainPos = new Vector3(0, 1, -2.41F);
    40.  
    41.         destinationCredits = Quaternion.Euler(targetCreditsRotX, targetCreditsRotY, targetCreditsRotZ);
    42.         destinationCreditsPos = new Vector3(targetCreditsPosX, targetCreditsPosY, targetCreditsPosZ);    
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         if (toCredits)
    48.         {
    49.             Debug.Log("X: " + targetCreditsPosX + " Y: " + targetCreditsPosY + " Z: " + targetCreditsPosZ);
    50.             Debug.Log("Target: " + destinationCreditsPos);
    51.             Debug.Log("Current: " + camera.transform.position);
    52.             camera.transform.rotation = Quaternion.RotateTowards(camera.transform.rotation, destinationCredits, 100F * Time.deltaTime);
    53.          
    54.             if (camera.transform.rotation == destinationCredits )
    55.             {
    56.                 //camera.transform.position = Vector3.MoveTowards(camera.transform.position, destinationCreditsPos, Time.deltaTime);
    57.                 camera.transform.position = destinationCreditsPos;
    58.                 if(camera.transform.position == destinationCreditsPos)
    59.                 {
    60.                     toCredits = false;
    61.                 }
    62.             }
    63.         }
    64.         if (toMain)
    65.         {
    66.             camera.transform.rotation = Quaternion.RotateTowards(camera.transform.transform.rotation, destinationMain, 100F * Time.deltaTime);
    67.             if (camera.transform.rotation == destinationMain)
    68.             {
    69.                 toMain = false;
    70.                 Debug.Log("ToMain");
    71.             }
    72.         }
    73.     }
    74.  
    75.     public void MoveToCredits()
    76.     {
    77.         animator.enabled = false;
    78.         toCredits = true;
    79.         toMain = false;
    80.     }
    81.  
    82.     public void MoveToMain()
    83.     {
    84.         animator.enabled = false;
    85.         toMain = true;
    86.         toCredits = false;
    87.     }
    88. }
    Thanks
     
    Last edited: Apr 23, 2019
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,201
    Sounds like you most likely have something else (some other script or behavior) moving the camera besides this script. Are you using Cinemachine maybe? Is the camera the child of some other object? Is an animation clip moving it?
     
    Kacper1263 likes this.
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,468
    Also be sure you understand the difference between world position and local position. If the target's local position in the Inspector is 1,2,3 but the target is really a child of another object far away from the origin, setting the camera to world coordinates 1,2,3 is not going to be what you want.
     
    dgoyette and Kacper1263 like this.
  4. Kacper1263

    Kacper1263

    Joined:
    Apr 7, 2019
    Posts:
    11
    Thank you for your help.
    Actually, if I used .localPositionit all started working. Although I tried to use localPositionit before, but the effect was the same so I gave up. Maybe it was because I did not save the script or I made another simple mistake.
    Thanks again!
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,201
    @halley made the right call.

    Just so you're clear, @Kacper1263, `transform.position' is a global position relative to the (0,0,0) center of the world. However, `transform.localPosition' is the object's position relative to its immediate parent. The values can sometimes be the same. For example, if an object doesn't have an immediate parent, then its position and localPosition values will be the same. But if (for example) you divide your game up into rooms, and each room has some stuff in it, and each room is at a different position, then the `transform.localPosition' of the objects in the room most likely won't match their `transform.position'.
     
    Kacper1263 likes this.
  6. Kacper1263

    Kacper1263

    Joined:
    Apr 7, 2019
    Posts:
    11
    Ok. Thanks for the clarification!