Forum begins after the advertisement:
[Part 2] character teleportation issue
Home › Forums › Video Game Tutorial Series › Creating a Farming RPG in Unity › [Part 2] character teleportation issue
- This topic has 6 replies, 4 voices, and was last updated 1 week, 3 days ago by
Tarha Apustaja.
-
AuthorPosts
-
June 25, 2025 at 9:46 pm #18385::
Hello Terresquall, I encountered a new problem in Part 2.
First of all, I guess the problems I had with Part 1 and Part 2 were because I used a different character model, since that was the only difference between mine and the tutorial.
The new problem appears in the Vector3.Lerp() code.
I tried changing “smoothing * Time.deltaTime” to “Time.time”, which fixed the problem. However, the jerky frame-by-frame issue still occurs.
But at least, this is much better than the character’s teleportation.
June 26, 2025 at 3:12 pm #18392June 26, 2025 at 9:54 pm #18393June 27, 2025 at 6:34 am #18394June 27, 2025 at 4:03 pm #18410::Sure.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { public float offsetZ = 5f; public float smoothing = 2f; //player transform component Transform playerPos; // Start is called before the first frame update void Start() { //Find the player gameobject in the scene and get its transform component playerPos = FindObjectOfType<PlayerController>().transform; } // Update is called once per frame void Update() { FollowPlayer(); } //Following the player void FollowPlayer() { //Position the camera should be in Vector3 targetPosition = new Vector3(playerPos.position.x, transform.position.y, playerPos.position.z - offsetZ); //Set the position accordingly transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.time); } }
I just changed “transform.position, targetPosition, smoothing Time.deltaTime” to “transform.position, targetPosition, smoothing * Time.time”.
June 28, 2025 at 3:15 pm #18411::@tarha.apustaja, what do you think? Can you have a look at his CameraController code and point out potential areas that could cause jerky-looking animations. It seems like it only happens when the character turns.
June 28, 2025 at 3:21 pm #18412::Sure, I can help you with that! To diagnose the issue with jerky-looking animations in your
CameraController
, it would be helpful to look at a few common areas that might cause this behavior:-
Camera Update Method: Check how the camera is being updated. If you’re directly setting the camera’s position or rotation in an update loop without smoothing, that can lead to jerky movements. Consider using interpolation methods like Lerp or Slerp for smoother transitions.
-
Frame Rate Dependency: If your camera’s movement is tied to the frame rate, it might cause inconsistencies. Ensure that the camera movement calculations account for the time between frames (delta time).
-
Character Turn Logic: If the character’s turning logic is abrupt (e.g., instant changes in direction), this can result in the camera following in a jerky manner. Ensure that the character’s rotation is smoothed out (using interpolation) before the camera follows.
-
Camera Positioning: If you’re positioning the camera based on the character’s position without any offset or smoothing, it can lead to abrupt movements. Make sure to use a smooth transition to adjust the camera’s position relative to the character.
-
Update Frequency: Ensure that your camera update logic runs at a consistent frequency. If it runs less frequently than the character updates, you may see jerky movements.
If you can share specific snippets of your
CameraController
code, I can provide more targeted advice!June 28, 2025 at 5:57 pm #18413::This is the code that was shared prior:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { public float offsetZ = 5f; public float smoothing = 2f; //player transform component Transform playerPos; // Start is called before the first frame update void Start() { //Find the player gameobject in the scene and get its transform component playerPos = FindObjectOfType<PlayerController>().transform; } // Update is called once per frame void Update() { FollowPlayer(); } //Following the player void FollowPlayer() { //Position the camera should be in Vector3 targetPosition = new Vector3(playerPos.position.x, transform.position.y, playerPos.position.z - offsetZ); //Set the position accordingly transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.time); } }
June 28, 2025 at 6:00 pm #18414::I noticed a critical issue in the camera smoothing calculation. The line:
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.time);
Should actually be:
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.deltaTime);
Using
Time.time
instead ofTime.deltaTime
will cause jerky camera movement becauseTime.time
continuously increases, making the interpolation speed exponentially faster over time.Time.deltaTime
provides a frame-independent smooth transition, which is what you want for camera movement.This small change should significantly improve the camera’s smooth following behavior, especially during character turns.
-
-
AuthorPosts
- You must be logged in to reply to this topic.