- Home /
The question is answered, right answer was accepted
Enemy Follow Player Without Rigidbody2D
Hello guys I just want to know is there a way to make enemy follow player without using rigidbody2D? Because my enemies just getting glitched when they collide with my player
Answer by WaqasHaiderDev · May 31, 2020 at 10:50 AM
Hi, you can use any of the following
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
or
transform.position = Vector3.Lerp(transform.position, target.transform.position, Time.deltaTime * speed);
But remember that in MoveTowards, your object will follow target at constant speed but in case of Lerp, your object will be fast at start but as the distance will reduce, it will start to decrease speed and will show ease in effect. Check how Lerp works otherwise tell me and I will explain to you.
I was using the first one so it didn't change anything and I don't want to use lerp.I am working on tower defense game that enemies come to you and you shoot with rigidbody attached bullets but my problem is when I shoot using bullet, bullet boosts them and enemies stop moving, and If I don't use rigidbody on enemies they overlap
I did not get this at all.
"but my problem is when I shoot using bullet, bullet boosts them and enemies stop moving"
what do you mean by boosting? Also movement should not stop as $$anonymous$$ove.towards will keep on moving TRANSFOR$$anonymous$$ towards the target. Share some gif or image if possible. Also share some more code.
here is the video link: https://youtu.be/$$anonymous$$ATTJCn05w0 if I dont use rigidbody on bullet It doesnt do any damage If I dont use rigidbody on enemies they just overlap
$$anonymous$$ake the bullet's mass really really low, and adjust the speed accordingly. Then depending on what you want the easy option is to just set the enemies velocity to be constant every frame rather than AddForce.
I just make the bullet's mass very low and it's worked thank you!
Answer by tadadosi · May 31, 2020 at 01:12 PM
You can achieve what you want without adding a rigidbody to your enemies:
Add a collider and a rigidbody to your bullet (marked as kinematic) .
Add a collider to your enemy marked as IsTrigger and create a new tag for your enemy which can be called Enemy.
Add logic to your bullet code to get the trigger collision.
Here is how the trigger collision code should look like in your bullet:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision != null)
if (collision.CompareTag("Enemy"))
{
// Play sound or any other stuff
// You could find your enemy script like this
// Enemy enemy = collision.GetComponent<Enemy>();
// and do something with it like enemy.AddDamage(bulletDamage);
}
}
With this method there are no forces involved in the process, just the function to get the collision.