fbpx

 

Looks like a proble...
 

Looks like a problem with unity not being able to cache my jump movement property???  

   RSS

0

Im struggling to get my jump to be smooth up and down instead it is snapping back down too fast as if when it gets updated in the next frame. I have followed the steps that were given in the 2.5D platformer video to fix that but its not fixing it. My code is exactly the same as his. When i run the same exact code on unity 2019.2 version it works exactly how it should. Any ideas on how to fix this?

Here is my code for a simple platform and capsule as player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
[SerializeField]
private float _speed = 5.0f;
[SerializeField]
private float _gravity = 1.0f;
[SerializeField]
private float _jumpHeight = 15.0f;
private float _yVelocity;

private CharacterController _controller;

void Start()
{
_controller = GetComponent();
}

void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
Vector3 direction = new Vector3(horizontalInput, 0, 0);
Vector3 velocity = direction * _speed;

if (_controller.isGrounded == true)
{
if(Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight;
}
}
else
{
_yVelocity -= _gravity;
}

velocity.y = _yVelocity;

_controller.Move(velocity * Time.deltaTime);
}
}

2 Answers
0

I am having a similar issue

Were you able to find a solution?

0

I think the problem here is indeed the framerate. There is compensation for how many frames it should take as you are falling faster each frame, and only the final velocity is affected by time. Not the acceleration by gravity.

It takes about 30-33 frames to fall when I tested it and it ran at about 2000fps with a frametime between 0.4 and 0.6ms (it got slower with debug logging).

So, unless you lock the framerate or compensatie for frametime doing it this way is not going to work well.

Easiest fix is to set Application.targetFrameRate = 60; somewhere in a Start method. If you are following the tutorial Player is the only script so set it there. It should now work and look like the video.

Share:

 

 

Scroll to Top

Please Login or Register