Forum begins after the advertisement:
[Part 1] Issue with Camera Follow Script
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 1] Issue with Camera Follow Script
- This topic has 2 replies, 3 voices, and was last updated 1 week, 1 day ago by
Ser Apustaja.
-
AuthorPosts
-
January 19, 2026 at 5:43 am #19205::
For some reason, my player started becoming invisible after I made the camera script. There’s no errors in the console. Do you know why this is happening?
SCRIPT:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFollow : MonoBehaviour { [SerializeField] private float followSpeed = 0.1f; [SerializeField] private Vector3 offset; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.position = Vector3.Lerp(transform.position, PlayerController.Instance.transform.position + offset, followSpeed); } }January 19, 2026 at 6:17 am #19206::It sounds like your player becoming invisible could be related to the camera’s positioning or the offset you are using in your
CameraFollowscript. Here are a few things to check:-
Check the Offset: Make sure that the
offsetyou have set in the inspector is not causing the camera to move too far away from the player. If the offset is too large or negative, it might be positioning the camera in a way that it moves through or away from the player, making them appear invisible. -
Camera Clipping Planes: Check the camera’s near and far clipping planes in the Camera component. If the player is too close or too far from the camera’s clipping planes, they may not be rendered. Adjust the near and far clipping planes to ensure they encapsulate the player’s position.
-
Player Visibility: Ensure that the player’s renderer (e.g., Sprite Renderer, Mesh Renderer) is enabled and that the player’s layer is not ignored by the camera. If the camera is set to only render specific layers, make sure the player’s layer is included.
-
Position of Player: Confirm that the player is not being moved to an unintended position in the game world. You can do this by temporarily disabling the camera follow script and checking where the player is located.
-
Debugging: You can add some debug logs to check the player’s position and the camera’s position during the update. For example:
void Update() { Vector3 playerPosition = PlayerController.Instance.transform.position + offset; transform.position = Vector3.Lerp(transform.position, playerPosition, followSpeed); Debug.Log("Camera Position: " + transform.position); Debug.Log("Player Position: " + playerPosition); } -
Camera Settings: Ensure that the camera’s settings (like projection type) are set correctly for your game. If you’re using a 2D game, make sure the camera is in Orthographic mode.
By checking these aspects, you should be able to identify the reason why your player is becoming invisible. If the issue persists, please provide more details about your player’s setup and the camera settings.
January 19, 2026 at 8:25 am #19207::I found the issue, the player keeps travelling in the z axis, no idea why that’s happening, but do you know how to fix that?
January 19, 2026 at 1:06 pm #19208::Issue solved. For anyone else facing the same issue, here’s what @gh0stproductions found:
Ah, I found the issue, the player keeps travelling forwards in the z axis, no idea why that’s happening Edit: I put the script inside the player, not the camera 🤦
January 19, 2026 at 1:55 pm #19211::If your player is unintentionally moving along the z-axis, there are a few potential causes and solutions you can explore:
-
Player Movement Script: Check your player’s movement script. Ensure that you are not inadvertently modifying the z position. For example, if you’re using input to control movement, make sure you’re only updating the x and y positions. Here’s a simple way to constrain movement to the x and y axes:
void Update() { float moveX = Input.GetAxis("Horizontal") * speed * Time.deltaTime; float moveY = Input.GetAxis("Vertical") * speed * Time.deltaTime; transform.position = new Vector3(transform.position.x + moveX, transform.position.y + moveY, 0); } -
Physics Settings: If the player is using Rigidbody, check if any forces or physics interactions are affecting the z position. You might want to freeze the z-axis movement in the Rigidbody component:
- Select your player GameObject in the Unity editor.
- In the Rigidbody component, find the “Constraints” section.
- Check “Freeze Position Z”.
-
Camera Follow Logic: If you want to ensure that the camera only follows the player’s x and y positions, you can modify your
CameraFollowscript to set the z position of the camera to a constant value. For example:void Update() { Vector3 playerPosition = PlayerController.Instance.transform.position + offset; transform.position = new Vector3(playerPosition.x, playerPosition.y, transform.position.z); } -
Check Colliders: If your player has colliders, they may be colliding with other objects that could push them along the z-axis. Make sure there are no unintended interactions with other colliders.
-
Debugging: Add debugging logs to track the player’s position during the game. This might help you identify when and why the z position is changing unexpectedly.
By implementing these changes, you should be able to restrict your player’s movement to the intended axes. If you continue to have issues, please share the relevant parts of your player movement code for further assistance!
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: