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
Unity Side - Attached PlayerMove script to Player Sprite, Set value for 2 class variables.
(Added components rigidBody2D and BoxColider2D to Player. Added BoxColider2D to other sprites.)
This script works. Player moves left, right and jumps.
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;
}
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.
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.
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.
The link above shows you how to use code-tags. They end up looking like this:
// Class Variables
private Transform playerTransform;
public float offset;
Regardless, the PlayerMovement code has an oddity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Project: PlayGround2
// Script: PlayerMovement
// Author: Dillard 04/19/2023
// Purpose: Allow player sprite to move left and right with keys
// Allow player sprite to jump
// Player sprite has 2d effects - Rigid body, box collider
// Ground sprite has 2d effects - box collider
public class PlayerMovement : MonoBehaviour
{
// Class Variables
public Rigidbody2D body;
[SerializeField] private float speed;
// Start is called before the first frame update
void Start()
{
body.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
// This code makes it go left and right (arrow keys) - It only affects the horizontal movement
body.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, body.velocity.y);
// This is the code that makes it jump - IT only affects vertical movement
if (Input.GetKey(KeyCode.Space))
body.velocity = new Vector2(body.velocity.x, speed);
}
}
Line 21, “body.GetComponent();” 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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
// Class Variables
private Transform playerTransform;
public float offset;
// Start is called before the first frame update
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;
//body = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
// Update is called once per frame (Update gets the position of all objects every frame)
// LateUdate is called after Update() and FixedUpdate(). (Takes the positions that update got and reacts to it.)
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;
}
}
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:
void LateUpdate()
{
Debug.Log("LateUpdate is running!");
// The camera position is the player position plus the X-axis offset.
transform.position = playerTransform.position + new Vector3(offset, 0f, 0f);
}