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

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #18385
    bao lex
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    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.

    #18392
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you share your camera controller script?

    #18393
    bao lex
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    Sure, I copied the code from your blog. Because in the first stage, I tried to restore the tutorial first, after that, I will learn to understand..

    The only change I made was to change “transform.position, targetPosition, smoothing Time.deltaTime” to “smoothing Time.deltaTime” to “Time.time”,

    #18394
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    0
    ::

    Could you paste it?

    #18410
    bao lex
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    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”.

    #18411
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    @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.

    #18412
    Tarha Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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:

    1. 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.

    2. 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).

    3. 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.

    4. 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.

    5. 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!

    #18413
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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);
        }
    }
    #18414
    Tarha Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    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 of Time.deltaTime will cause jerky camera movement because Time.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.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: