How to make Rigidbody.AddForce less delayed in Unity3D?

Game Development Asked on January 5, 2022

I’m trying to make it so that when the player in my game moves left or right, he lightly jumps from his current position to his new position. The way I have it set up now, the jump occurs once he has arrived at his new position. What can I do to make it so that the jump happens sooner so that it appears that he jumps to his new position? Here is the code I have that controls that part of his movement:

if (Input.GetKeyDown(KeyCode.LeftArrow) && transform.position.x > minXPos)
{
targetPos = new Vector3(transform.position.x - xIncrement, transform.position.y, transform.position.z);
transform.position = Vector3.MoveTowards(targetPos, transform.position, speed);
isHorizontalingLeft = true;
}
else if (Input.GetKeyDown(KeyCode.RightArrow) && transform.position.x < maxXPos)
{
targetPos = new Vector3(transform.position.x + xIncrement, transform.position.y, transform.position.z);
transform.position = Vector3.MoveTowards(targetPos, transform.position, speed);
isHorizontalingRight = true;
}


Here is a video showing what is happening (sorry it’s blurry):
https://youtu.be/rPewtb8QywE

I can't tell from a video when you're applying input, but it looks to me like your problem is not that the jump is delayed, but rather that your character teleports instantly instead of moving over a period of time.

Your code moves the player like this:

transform.position = Vector3.MoveTowards(targetPos, transform.position, speed);


I am assuming your code takes place in an Update() function. You are not accounting for Time.deltaTime here, so your character may be moving 60x faster than you expect. Normally your speed should be in units per second and you should multiply speed by Time.deltaTime like this:

transform.position = Vector3.MoveTowards(targetPos, transform.position, speed * Time.deltaTime);


This way your movement speed is consistent regardless of framerate. For example, if the speed is 10 and a frame lasts 1/60th of a second, the actual distance moved in one frame would be 10 * (1 / 60) = .16667. If the game stuttered during the next frame and the frame lasted 2/60ths of a second, the distance moved for that frame would be 10 * (2 / 60) = .333333

Answered by Kevin on January 5, 2022

Related Questions

How can I make particles move like a square in Unity?

1  Asked on December 21, 2021 by magikarpsama

Unity3D URP – How do I approach creating Fog of War for 3D top-down stealth game?

1  Asked on December 21, 2021 by zekko

How can I make the text flash?

1  Asked on December 20, 2021

Transform.Rotate is jumping around

1  Asked on December 18, 2021

World space coords for 2d images

1  Asked on December 15, 2021 by user3134909

How do I safely change properties on my skybox?

1  Asked on December 15, 2021

Why does masked animation look so strange?

1  Asked on December 15, 2021

Save-security with MD5 hash

3  Asked on December 13, 2021 by abdulla

Rigidbody player controller on moving platforms

1  Asked on December 13, 2021 by gorgo-3

Can I bring features of an Android game into another Android game?

1  Asked on December 13, 2021 by thinkgood

Polygon filling algorithm

0  Asked on December 13, 2021

How to programmatically overlay a shader on a terrain by mask (Unity)?

0  Asked on December 13, 2021

1  Asked on December 11, 2021 by single-arrow-games

What is “Avatar” in the “Animation Controller” used for?

0  Asked on December 11, 2021

2D Grid-based Pathfinding with Elevation

1  Asked on December 11, 2021 by oyed

Mirror/Unet, RegisterSpawnHandler throws invalid asset Id error because assetId is 0 for dynamically loaded assetbundles

1  Asked on December 8, 2021

Placing Zones in Cities Skylines

1  Asked on December 8, 2021 by fence_rider

What causes skating feet in Unity (but not in mixamo or Autodesk FBX review)?

1  Asked on December 8, 2021

Frame-rate drops when drawing 2D map tiles

1  Asked on December 6, 2021 by theco

Default Game Window Size To Take While Making Games

2  Asked on December 6, 2021