- Home /
AddForce not working on Y
Hi, so I made an enemy that should jump at player when he is near "attack" range, I used rigidbody.AddForce to do that. When I add force x and z works perfectly but when I do y it doesnt do anything.
Code: if (states == theState.attack) { if (Vector3.Distance(transform.position, player.transform.position) > 10f) { states = theState.run; agent.enabled = true; }
transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
agent.enabled = false;
//Buggy Part
rigi.AddForce((player.transform.position.x - (transform.position.x)), 10f, (player.transform.position.z - (transform.position.z)), ForceMode.Impulse);
agent.enabled = true;
StartCoroutine(noVelocity());
states = theState.run;
}
Answer by NikkF · Mar 14 at 06:22 PM
AddForce doesn't work like "moving it up by 10 on the y axis" as you wrote.
Instead, you have to specify a direction (in your case Vector3.up if you want it to always go up, or transform.up if it should consider object rotation) and multiply that by a force value.
Here's the difference between transform.up and Vector3.up:
Use something like this instead:
rigidBody.AddForce(Vector3.up * force, ForceMode.Impulse);
Hope this helps!
Answer by TheRandomPerson6 · Mar 14 at 06:32 PM
Hi, I tried " rigi.AddForce(transform.up * 20f, ForceMode.Impulse);" it but it doesnt seem to work. Do you got other solutions? Cheers
Well, in that case, I assume your object is either static or kinematic. Make sure that the RigidBody has isKinematic set to false as this will prevent your object from doing physics.
If that also doesn't work, then I assume it's an issue with something I can't see here.
Hi, so the bug got caused because of what you said and I immediatly activated navMeshAgent right after adding force.
Sorry to waste your time :(
Your answer
