Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question transform.position is not working in my camera tracker

Discussion in 'Scripting' started by reacharnavsaraf, Jun 7, 2023.

  1. reacharnavsaraf

    reacharnavsaraf

    Joined:
    Jun 6, 2023
    Posts:
    4
    Hi, I am new to unity and I was using the tutorial code given for 2d camera tracking. It came in two seperate c# files. The first one was added on the player it's code shown below:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Movement : MonoBehaviour
    4. {
    5.     public float speed = 20;
    6.     private Vector2 motion;
    7.  
    8.     // Update is called once per frame
    9.     void Update(){
    10.         motion = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    11.         transform.Translate(motion * speed * Time.deltaTime);
    12.     }
    13. }
    14.  
    The second file is attached to an empty game object and for this file it gives me an error on line 21, collum 59, that transform.postion isn't valid or something. Here is the code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Tracker : MonoBehaviour
    4. {
    5.    
    6.     public Transform trackedObject;
    7.     public float updateSpeed = 3;
    8.     public Vector2 trackingOffset;
    9.     private Vector3 offset;
    10.  
    11.     void Start()
    12.     {
    13.         offset = (Vector3)trackingOffset;
    14.         offset.z = transform.position.z - trackedObject.postion.z;
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void LateUpdate()
    20.     {
    21.         transform.postion = Vector3.MoveTowards(transform.postion, trackedObject.position + offset, updateSpeed * Time.deltaTime);      
    22.     }
    23. }
    24.  
    Here is the link to the tutorial: Controlling Unity Camera Behavior - Unity Learn
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    u must drag player on trackedobject
     
  3. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    There is also no script solution by just dragging and dropping the camera inside the player's game object (to his hierarchy). The player moves, and the camera automatically follows.
     
  4. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    483
    That's because on line 21 you have typed
    transform.postion
    instead of
    transform.position
    . Not just once, but twice on that same line.

    It looks like you have also put
    postion
    instead of
    position
    on line 14 as well.

    Correct spelling matters.
     
    MelvMay likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    As above, you need to have no compiler errors before you can run it. At first glance I thought you had a problem running it. :)

    If in doubt as to what is available on any component or API, you can use the scripting reference.

    Here's the Transform.position page.