- Home /
How to stop an rigidbody enemy from jumping when chasing the player?
Hello, I am developing a simple fps game where the player is on a map with some hills and a forrest. Enemies spawn round the edges of the map and walk towards the player. The player has to shot them 5 times to kill before they reach him. The player can move, I used a First Person Controller.
The problem I am facing is that the enemies start to jump around and glich when they are waling towards the player. There is no errors in the console when the game is running. This is the code used for the enemies:
#pragma strict
var player:Transform;
var speed:float;
function Start()
{
player = GameObject.Find("First Person Controller").gameObject.transform;
}
function Update () {
transform.LookAt(Vector3(player.position.x , 0.0 ,player.position.z));
transform.position += Vector3(transform.forward.x,0,transform.forward.z)*speed*Time.deltaTime;
}
For the enemies I used a model made in wings3D. The model is in a shape of a zombie with its arms stretched out in front of it. I have set colliders to each part of the model (box colliders) and made the whole thing rigidbody. As you can see in the code there is nothing in the code that would make the enemy jump and glich out. The code worked without any problem when the enemy was only a cube. This makes me think that it is the physics of the model that are causing the problem.
How do I stop the enemy to jump and glich when approaching the player?
Thank You, Dachckol
"Jump" in what sense? Up in the air? Or do they teleport ahead faster than they should?
They jump up in the air, in some cases they continue flying forever and in some they fall.
Answer by nikescar · Aug 16, 2012 at 11:01 PM
You are correct. It is the physics that is causing the problem. You said that you attached rigidbodies to the zombie game objects. When you do this the physics system starts to control their position. The problem comes in when you override the physics by using transform.position. Then the physics engine has to catch up to the new position and it's all, "What kind of sorcery is this?"
To move a physics object you should use something like AddForce.
I haven't mess with character movement before but something like...
var speedModifier : float = 10;
rigidbody.AddForce((player.position-transform.position) * speedModifier);
...may be a good place to start.
Thank You for the reply. I have used the code you provided but I added a *Time.Delta time after multiplying the direction by the speed.
After configuring the mass and the speed of the enemy everything started working.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Best way for rigidbody enemy? 1 Answer
How do you add a fixed joint to a first person controller 1 Answer
Projectile Boomerang 1 Answer