For prototyping purposes, you could always use a coroutine. It will get things moving, but I would try to structure your code so physics are in a fixed update, eventually.
A coroutine is basically a process that runs on the side. It gives you enough flexibility to get the jump working while you fix your enemies. Be careful with coroutines though, I have seen some get way out of hand. You don't want too many, or too much going on in one. Memory leaks can happen here.
This is not how I would handle a jump, but it works. I would consider utilizing a state pattern for your character (even a small pseudo pattern could work.) This is not a terrible approach to a jump, though.
I am not in a place where I can test this, so this is just an example off the top of my head to show you what the syntax would look like.
void Update(){
HandleInput(this);
}
private void HandleInput(IControllable controller){
...
if(controller.jump)
StartCoroutine(HandleJump());
}
// jumpHeightVector is a vector 3 with y being your jump height applied, the rest 0
private Vector3 jumpHeightVector = (0, jumpHeight, 0);
// every 10th of a second increments current time by 1
private float jumpInterval = 0.1f; // this would be responsible for a choppy movement
[SerializeField] jumpTime; //serialize this to show in inspector
private IEnumerator HandleJump(){
// Important: let update know that this method is off limits
controller.jump = false;
anim.SetBool("jump", true);
curJumpTime = 0;
while(curJumpTime < jumpTime)
{
// may need better validation for landing in the right spot
if(curJumpTime * 2 < jumpTime) Transform.Translate(jumpHeightVector); // up
else Transform.Translate(-jumpHeightVector); // down
// this always creates a new wait for seconds, consider assigning one of these in Start()
yield return new WaitForSeconds(jumpInterval);
curJumpTime++;
}
anim.SetBool("jump", false);
}
We have a coroutine that loops through a while loop while current jump time curJumpTime
is less than our allowed jump time jumpTime
. if we are less than half of the way through our jump, move up. If we are less than half, move down. When our elapsed jump time is equal to our allowed jumptime, we break the while loop. We also set our animation bool (you could even just use anim.Play("jump")
.
enemies
layer and have used layer collision matrix but didn't think about adding anenemies_onair
layer. thanks for the link! \$\endgroup\$