Forum begins after the advertisement:
[Part 1] errors on velocity and victor
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 1] errors on velocity and victor
- This topic has 11 replies, 3 voices, and was last updated 3 weeks, 5 days ago by
Terence.
-
AuthorPosts
-
April 15, 2025 at 2:09 am #17914April 15, 2025 at 2:11 am #17916::
If the camera is moving on its own when you press play, it could be due to several reasons. Here are a few things you can check:
-
Camera Follow Logic: Ensure that your camera follow logic is correctly implemented and not causing the camera to jump unexpectedly. If you’re using
Lerp
orSmoothDamp
, make sure the target position is being calculated correctly. -
Multiple Camera Scripts: Check if there are multiple scripts attached to the camera that might be affecting its position. Disable or remove any other scripts that could be influencing the camera’s movement.
-
Parenting Issues: Ensure that the camera is not a child of another object that is moving. If it is, it will inherit the movement of that parent object.
-
Player Position: Check if the player’s position is being altered in a way that causes the camera to react. For example, if the player starts at an unexpected position or is moving rapidly, the camera might follow it in a way that seems erratic.
-
Update Method: Check if you are using
FixedUpdate()
instead ofUpdate()
for your camera follow logic. The camera should ideally follow inUpdate()
to ensure smooth movement.
Here’s an example of a simple
CameraFollow
script:public class CameraFollow : MonoBehaviour { public Transform player; // Assign the player transform in the Inspector public Vector3 offset; // Assign offset in the Inspector public float followSpeed = 0.1f; void LateUpdate() { Vector3 targetPosition = player.position + offset; transform.position = Vector3.Lerp(transform.position, targetPosition, followSpeed); } }
Make sure to assign the
player
variable in the Inspector.Does any of this help clarify the issue? If the problem persists, please share your
CameraFollow
script for further assistance.April 15, 2025 at 2:17 am #17917::i used the last one but now the camera is not following the player. what do i need to type in player controller script?
April 15, 2025 at 2:21 am #17918::In your
PlayerController
script, you need to ensure that thePlayerController
instance is properly set up as a singleton so that theCameraFollow
script can access it. Here’s a reminder of how to implement the singleton pattern in yourPlayerController
:public class PlayerController : MonoBehaviour { public static PlayerController Instance; private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } } // Other player movement code... }
Make sure this code is included in your
PlayerController
script. This will allow theCameraFollow
script to reference the player correctly.Additionally, ensure that you have assigned the
player
variable in theCameraFollow
script to reference thePlayerController
instance. You can do this in the Unity Inspector by dragging the player GameObject into theplayer
field of theCameraFollow
component.Is your
PlayerController
script set up correctly with the singleton pattern, and have you assigned the player reference in theCameraFollow
script?April 15, 2025 at 2:28 am #17919April 15, 2025 at 2:31 am #17920::If the camera is still moving away from the player despite following the previous suggestions, let’s troubleshoot further:
-
Check Offset Values: Make sure that the
offset
values are set correctly. If the y value is too high, the camera will appear to move away from the player. For example, if your player is at (0, 0, 0), an offset of (0, 5, -10) will place the camera above and behind the player. -
Camera Position Reset: Sometimes, the camera’s initial position may not be set correctly. Try resetting the camera’s position in the Scene view to ensure it starts at the expected location relative to the player.
-
LateUpdate Method: Make sure you’re using
LateUpdate()
for the camera follow logic. This ensures that the camera position updates after the player’s position updates. -
Player’s Position Changes: Check if there are any scripts affecting the player’s position that may be causing erratic movement. Ensure that the player is not being moved in a way that would push it away from the camera.
-
Debugging Position: Add some debug statements in your
CameraFollow
script to log the camera and player positions:void LateUpdate() { Vector3 targetPosition = player.position + offset; transform.position = Vector3.Lerp(transform.position, targetPosition, followSpeed); Debug.Log($"Camera Position: {transform.position}, Player Position: {player.position}"); }
-
Camera Clipping Planes: If the camera is moving too far away, check the camera’s clipping planes. Ensure the Near and Far clipping planes are set correctly; although this typically wouldn’t affect position, it’s worth checking.
-
Camera Constraints: If you have any constraints or additional logic affecting camera movement, such as limits on position or rotation, make sure they are not causing the camera to behave unexpectedly.
If you’ve checked everything and it still doesn’t work, please share your complete
CameraFollow
script and any relevant parts of yourPlayerController
script so we can take a closer look.April 16, 2025 at 1:27 pm #17931::@yentlblanckaert can you share your full
CameraFollow
script? You probably have some part in your script where the follow position is incorrectly set. -
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: