- Home /
Prevent objects from pushing eachother on collide
I have an enemy and a player in a 2D game. The enemy moves to the players position automatically.
I have a box collider set to both enemy and player to prevent them from moving inside eachother. I have a circle collider set to both the enemy and player set to trigger.
When the enemy and the player collide they can push eachother, example:
The enemy automatically walks to the player, when they collide the enemy continues to walk to the players position therefore pushing the player across the screen.
Example 2:
When the player walks up to the enemy (with the follow script turned off) the player can push the enemy across the screen.
ALSO:
When I get the above fixed how do I get the enemy to stop trying to move to the players position when it is next to the player?
I ran a little test where I made the player kinematic so when the enemy touched to the player he doesn't move. BUT you can visible see the player is still trying to the push the player to get to his position as the image judders.
How do I prevent this both of these?
This is the follow script to make the enemy move to the players position:
if (enemyHealth.enemyCurrentHealth > 0 && playerHealth.currentHealth > 0)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
Thanks!
Answer by lloladin · Sep 24, 2015 at 08:51 AM
so the way that i understand this is you want the enemy to stop moving torwards the player when it gets too close to the player and you want the play not to push the enemy when colliding?
there are many ways to do this i personally like "Linecast" which pretty much just draw a line between to postions which you can use to detect things like ground,enemies,walls etc so to make youre enemy stop pushing the player after getting close you can do soemthign like this
// display range in scene
Debug.DrawLine (new Vector3(transform.position.x - PlayerRange, transform.position.y, transform.position.z), new Vector3(transform.position.x + the length of the line, transform.position.y, transform.position.z));
// if player is which in range and on left side
if (transform.localScale.x > 0 && player.transform.position.x < transform.position.x && player.transform.position.x > transform.position.x - the length of the line)
{
playerInRange = true;
}
and then changing youre code
if (enemyHealth.enemyCurrentHealth > 0 && playerHealth.currentHealth && !PlayerinRange> 0)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
this will make the enemy only move while the player is not in range
the second problem i gues you could save the enemys position or soemthing when he stops moving and then
if (PlayerinRange)
{
transform.position = SavedPosition/Vector2D;
}
hope this helps you :)
Your answer
Follow this Question
Related Questions
Can't detect cursor collision when moving too fast (2D) 1 Answer
Collider2D.GetContacts(List() contacts) works with OnColliderEnter2D but not with OnTriggerEnter2D 1 Answer
2d rigidbody falling from the collider when it is moved 3 Answers
Use transform.position in FixedUpdate to ensure collision with objects which have Velocity 0 Answers