简体   繁体   中英

Unity3D c# 2D Game Camera Follow is making jumps

I made an 2D endless runner game using Unity, where the Player Object moves upwards, but for the Viewer he remains on the same spot because the Camera is following him. The Following works pretty well, but the problem is that it is making small jumps every few seconds. The jumps are small and thus are not affecting the gameplay in general but they are big enough to make it seem like the game is not running smoothly.

I can't even tell whether it is the Camera that is making the jumps or the Player Object. But I think it is the Camera, because the spawning obstacles are also making jumps. What may be the issue? Am I using the right method for the Camera?

public class Player
{
    float speed = 5f;
    void Update()
    {
        transform.Translate(0, speed*Time.deltaTime, 0);
    }
}

And this is the Script which is attached to the Camera:

public class CameraFollow
{
    private Vector3 FollowVector;
    public Transform Player;
    void LadeUpdate()
    {
        FollowVector = Player.position - new Vector3(0, -4f, 10);
        transform.position = Vector3.Lerp(transform.position, FollowVector, Time.deltaTime * 4f);
    }
 }

Does the jump happen every 4 seconds? I think the problem might be in your Vector3.Lerp function. It's basically a loose camera-follow, that takes 4 seconds ( Time.deltaTime * 4f ) to smoothly go from Point A to point B. Every 4 seconds it resets its path, hence the hop. At least that's my theory.

If you don't mind the camera rigidly following the player, I would just remove the Lerp and just set the transform.position to be the FollowVector value like so:

void LadeUpdate()
    {
        transform.position = Player.position - new Vector3(0, -4f, 10);
    }

Try to use Vector3.MoveTowards

float step =  speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, Player.position - new Vector3(0, -4f, 10f), step);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM