- Home /
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 =(
I would suggets to use a Nav$$anonymous$$eshAgent for that. You should check the "Survival Shooter tutorial" from Unity. It shows exactly what you need:
https://unity3d.com/learn/tutorials/projects/survival-shooter-tutorial
Part 4 is about the enemy behavior: https://unity3d.com/learn/tutorials/projects/survival-shooter/enemy-one?playlist=17144
Answer by ProgramacionWH · Sep 07, 2016 at 12:29 AM
Changing position " transform.position += transform.forward*MoveSpeed*Time.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.
What do you recommend to use in the addforce method? I didn't find any source of this command in Javascript, only in C#
Answer by millej23 · Sep 07, 2016 at 12:29 AM
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!
Yes, i am using rigidbody. I think that if i use navmesh it would be strange. As far as i know, navmesh is predictated, right? I needed something that followed the player. Anyway, thanks =)
Answer by unityBerserker · Sep 07, 2016 at 12:02 AM
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).
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.
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.
https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
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).
Thank you for the hints, the FixedUpdate ins$$anonymous$$d of just Update helped a lot, the movements are much more smooth now, but the enemy is still ignoring the physics, as it doesn't even move when i hit it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612092317im_/https://answers.unity.com/themes/thub/images/avi.jpg)