So I’ve been trying at this for a while now but I always fix an issue then another pops up. With each fix my code gets messier and messier. Basically, I’m creating a slime enemy in 3D that uses a navmesh agent to calculate a path but then I want to use rigidbody.addforce to jump in the direction it’s facing. The hard part is disabling the navmesh, enabling the rigidbody, adding the force, wait for the force to be applied before renabling the navmesh before it repeats.
My code is basically this:
Update() {
if (isGrounded) {
//Turn off navmesh
//Turn on Rigidbody
//Add force
//Turn off rigidbody
//Turn on navmesh
//coroutine to space out
//jumps
The problem I’m running into is it is reactivating the navmesh before all the force has been applied. I thought maybe another coroutine but that seems sloppy and not very great. Suggestions?
you could try using a toggle were you
if (isGrounded)
{
//Turn off navmesh
//Turn on Rigidbody
//Add force “an impulse force”
}
if you are using a ground check like a raycast or physics overlap the isGrounded bool would then go false
then the next time you hit the ground the bool is set true
so something like
if (isGrounded==false)
{
if (//your ground check function so a raycast or a physics overlap)
{
//Turn off rigidbody
//Turn on navmesh
//coroutine to space out
isGrounded=true;
}
}
could work
This has been recommended before and while it seemed like it would work it doesn’t. I use a physics overlap for my ground check and it doesn’t even get to apply any amount of force before it turns on the nav mesh again because of how fast code naturally executes. It’ll apply the force then turn on the nav mesh in the same second.
ok so an extra bool should be all you need
if (isGrounded&&canJump==true)
{
//Turn off navmesh
//Turn on Rigidbody
//Add force
invoke( nameof (ResetJump),small delay)
}
// this if statement prob not needed if (isGrounded==false)
{
if (ground check function)
{
if(canJump==false)
{
//Turn off rigidbody
//Turn on navmesh
//coroutine to space out
canJump=true;
}
isGrounded=true;
}
}
ResetJump ()
{
canJump=false
}
should work
should fix you computer working to fast because of invoke delay.
if this dose not work you could also try putting the functions in FixedUpdate
1 Like