Non-physics based player inertia while jumping

Hey everyone, first time posting a question here, I’ll try to be as clear as possible.

I have a character system that uses transform.translate to mode in the X and Z directions, but physics for the Y direction (jumping).
The reason for this is I don’t want any momentum for the character when they stop moving, and I have coded in a custom initial speed up system that I don’t want to rely on unity’s physics for accuracy.

Obviously, when the character jumps, they should continue moving at the same speed and direction they were going (not necessarily vector3.forward).

To do this, I tried to code in a DeltaX and DeltaZ function, that updates every-other frame:

if(abovegr == false  jumping == false  startedjump == false)
	{
		movex = (transform.position.x - delx)/Time.deltaTime;
		movez = (transform.position.z - delz)/Time.deltaTime;
		jumptimer = 60;
		if(nextx == 0)
		{
				delx = transform.position.x;
				delz = transform.position.z;
			nextx = 1;
		}
			nextx--;
	}

Then when the character is jumping, the following X and Z translations are used:

if((abovegr == true || jumping == true)  gliding == false)
	{
		transform.Translate(Vector3(movex*Time.deltaTime,0,movez*Time.deltaTime),Space.World);
	}

(There is a bit more code that stops the character from continuing to attempt to fly in a direction after coming into contact with an object mid air, but my problem doesn’t stem from the other parts)

This tends to work most of the time, but every so often, the character either doesn’t move at all, or doubles the speed.
I assume it is because there are framerate changes that affect the deltaX and deltaZ between multiple frames. (I tried Time.smoothDeltaTime, but it didn’t seem to have any effect)

Is there a better way to implement a DeltaX and Z system? I’d like not to modify it too much since the system works fairly well as is, but if there is a different way to approach other than having to use:

if(nextx == 0)
		{
				delx = transform.position.x;
				delz = transform.position.z;
			nextx = 1;
		}
			nextx--;

that would be nice.

Thanks in advance!

  • Blueteak

my question would be, you just wanted a different behavior as in making your own physics character?

I don’t want actual physics based inertia, just for the character to travel in the same direction he was right before he jumps.