# Make enemy follow and hit the player

Hello everyone, sorry for making SO MANY questions in one day, i am curious and i want to learn xD

I am making a scene where the player is a sphere, and he has an enemy sphere that need to “kill” him. I have a script that i’ve found that allow the sphere to follow the player, but it’s literally impossible.

The enemy sphere COMPLETELY ignores any obstacle, can fly, it doesn’t seems to care about physics, it’s just impossible to run away from it. I am using a sphere controller based on the Unity’s rollerball.

So, do anyone know what do i need to change in the script so it “respects” the standard physics, and move more like a player, and not just fly around in a way that it appear that it is rolling?

Here’s the script, it’s JavaScript:

`````` var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;

function Start ()
{

}

function Update ()
{
transform.LookAt(Player);

if(Vector3.Distance(transform.position,Player.position) >= MinDist){

transform.position += transform.forward*MoveSpeed*Time.deltaTime;

if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}

}
}
``````

I would be grateful if anyone helped me. I am a beginner, and unfortunely, i don’t know anyone that could teach me anything, and there are no courses,universities,etc… in my city. The tutorials in youtube and the documentation help, but not much =(

Changing position " transform.position += transform.forwardMoveSpeedTime.deltaTime;" ignores physics it is like a teleport .

So you have to use a Character Controller or a Rigidbody Components.
In the case of a rigidbody you can add force to it:

``````gameObject.GetComponent.<Rigidbody>().AddForce(transform.forward ... etc);
``````

If you use rigidbody, remember to change its Drag value (How much air “stop” the movement of the object) to a higher value in order to make it inertia smaller.

Hello!

Have you added a rigidbody and collider to the enemy object?

Also, if you are wanting the enemy to avoid obstacles, etc, you may need to setup a NavMesh agent. See here:

https://docs.unity3d.com/Manual/nav-BuildingNavMesh.html

Let me know if that helps at all, thanks!

@Zitoox

1. If you want to use physics on object you should add Rigidbody and Collider components.
Rigidbody simulate physics( like reaction to forces), colliders - react with other object (like blocking them).
2. Rigidbody(NvidiaX -physic engine) and Rigidbody2D(Box2D) don’t interract with themselves. These are using two different physic engines. You can only interact components with same ending of component name (2D or none).
Same rules are for colliders.
3. If you move object it need Rigidbody component. In that component is isKinematic property.
If this option is unchecked object react with other object ( to forces) - to move that object you should use .addForce method.

If isKinematic is set to true - object not react with forces. You should move that gameobject it with transform.

4. Colliders have ability to colliding with other gameobject (both must have colliders). Colliders has property isTrigger. If we mark is as true - this object will not react with others colliders.

5. If you moving object using physics you should do it in FixedUpdate() not Update() - movement will be more fluent.

Those are most important thing for begging (but not all).