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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Camera Follow Code worked in 2022 - spring, didn't work in 2022-23 school year - help!

Discussion in 'Scripting' started by edillard, Apr 19, 2023.

  1. edillard

    edillard

    Joined:
    Feb 4, 2022
    Posts:
    6
    PlayerMove Code: https://drive.google.com/file/d/1uSbqYyZMMWm-6gOJSfXAfVsgE7zjGuYr/view?usp=share_link
    CameraFollow Code: https://drive.google.com/file/d/1xRK8LECtg3pi0-y4KC3lhXvfD4bJpCWD/view?usp=share_link
    - what you want - I want to have the camera follow the player sprite (In CameraFollow code above)
    - what you tried - Code snippet below or see attached code
    - what you expected to happen -Camera to follow player sprite: last year this code worked - the camera followed the sprite.
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    No error message. It was like the code wasn't there. (RE Camera Follow - It was attached to the camera)
    SETUP
    Set up on Unity side

    1) Unity Side - Attached PlayerMove script to Player Sprite, Set value for 2 class variables.
    2) (Added components rigidBody2D and BoxColider2D to Player. Added BoxColider2D to other sprites.)
    This script works. Player moves left, right and jumps.
    3) Unity Side - Attached CameraFollow script to camera. Tagged Player as player
    The CameraFollow script produces no errors but does not work as expected.
    Code that was attached is linked above.
    Code snippets below.

    [code/code]
    // Class Variables
    private Transform playerTransform;
    public float offset;

    // In Start method
    void Start()
    {
    //This finds a game object that is tagged as player and pulls in it's transform data.
    //In Unity I tagged the Player object with the Tag - Player
    playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    }

    // In late update
    void LateUpdate()
    {
    // I need to capture the position of the camera in a temp variable.
    Vector3 temp = transform.position;
    // I need to modify the position of the camera, using the temp variable to account for the Players position.
    temp.x = playerTransform.position.x;
    temp.x += offset;
    temp.y = playerTransform.position.y;
    // I need to reset the position of the camera, accounting for the players position.
    transform.position = temp;
    }
     
    Last edited: Apr 20, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.

    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/
     
  3. edillard

    edillard

    Joined:
    Feb 4, 2022
    Posts:
    6
    I am using this for a classroom - there is a limit to what I can install on the school machines.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Cinemachine is a standard Unity package available in the package manager, it's not a separate installer. Your existing project will be comprised of packages that it downloaded.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    If the code above is in a script, please post the full script using code-tags as requested above although TBH, your debugging should verify that this script is on a GameObject in the scene. You can easily verify if the "LateUpdate" is running by adding a Debug.Log() to it.

    Nobody can debug or tell you what is wrong from a plain-text code snippet really.
     
  6. edillard

    edillard

    Joined:
    Feb 4, 2022
    Posts:
    6
    -------
    I can't add CineMachine at this point - I will review it for next year. This is a high school computer lab - and
    we try not to add stuff during the semester unless we absolutely have to.
    I think I have the material updated that you asked for - not sure if I got the tags right. Put links to 2 .cs files.
    Added more detail. Added [code/code] tag at start of code snippets.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    The link above shows you how to use code-tags. They end up looking like this:
    Code (CSharp):
    1. // Class Variables
    2. private Transform playerTransform;
    3. public float offset;
    Regardless, the PlayerMovement code has an oddity:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. // Project: PlayGround2
    5. // Script:  PlayerMovement
    6. // Author:  Dillard  04/19/2023
    7. // Purpose: Allow player sprite to move left and right with keys
    8. //          Allow player sprite to jump
    9. //          Player sprite has 2d effects - Rigid body, box collider
    10. //          Ground sprite has 2d effects - box collider
    11.  
    12. public class PlayerMovement : MonoBehaviour
    13. {
    14.     // Class Variables
    15.     public Rigidbody2D body;
    16.     [SerializeField] private float speed;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         body.GetComponent<Rigidbody2D>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         // This code makes it go left and right (arrow keys) - It only affects the horizontal movement
    28.  
    29.         body.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, body.velocity.y);
    30.  
    31.         // This is the code that makes it jump - IT only affects vertical movement
    32.         if (Input.GetKey(KeyCode.Space))
    33.             body.velocity = new Vector2(body.velocity.x, speed);
    34.  
    35.     }
    36. }
    Line 21, "body.GetComponent<Rigidbody2D>();" gets the Rigidbody2D component you've presumably set in the inspector and then grabs itself but doesn't do anything with the results. You can remove this line, it does nothing.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6. {
    7.     // Class Variables
    8.     private Transform playerTransform;
    9.     public float offset;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         //This finds a game object that is tagged as player and pulls in it's transform data.
    15.         //In Unity I tagged the Player object with the Tag - Player
    16.  
    17.         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    18.         //body = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.      
    25.     }
    26.  
    27.     // Update is called once per frame (Update gets the position of all objects every frame)
    28.     // LateUdate is called after Update() and FixedUpdate().  (Takes the positions that update got and reacts to it.)
    29.  
    30.     void LateUpdate()
    31.     {
    32.         // I need to capture the position of the camera in a temp variable.
    33.         Vector3 temp = transform.position;
    34.         // I need to modify the position of the camera, using the temp variable to account for the Players position.
    35.         temp.x = playerTransform.position.x;
    36.         temp.x += offset;
    37.         temp.y = playerTransform.position.y;
    38.         // I need to reset the position of the camera, accounting for the players position.
    39.         transform.position = temp;
    40.     }
    41. }
    So as above, you need to DEBUG this yourself. We don't have the project, it's not fully described by this code.
    • Is the LateUpdate running? Add a Debug.Log to verify
    • If so then is the playerTransform set to the thing you are tracking. Verify! If you have more than a single GO with that tag, you won't be getting the one you think. As a test, make the "playerTransform" field public and drag the correct one into it.
    A small thing but you can simplify the code:
    Code (CSharp):
    1. void LateUpdate()
    2. {
    3.     Debug.Log("LateUpdate is running!");
    4.  
    5.     // The camera position is the player position plus the X-axis offset.
    6.     transform.position = playerTransform.position + new Vector3(offset, 0f, 0f);
    7. }
    Hope this helps.