playMaker

Author Topic: [SOLVED] Push enemy to opposite direction on collision  (Read 7678 times)

heavygunner

  • Sr. Member
  • ****
  • Posts: 344
[SOLVED] Push enemy to opposite direction on collision
« on: April 03, 2020, 11:50:29 AM »
Hello !

See the pic. let's say Player and Cube colliding. Once Collided, I will test a condition. If it is true, Cube should be pushed back. If false, Player should be pushed back.

I couldn't get it done. I can bale to test Condition. But, not able to push back.
I got collision info and added force on Normal vector. Not working

 Both objects have Ridgidbody and Collider. Bot Mass 30 and Drag 10

Thanks
« Last Edit: April 05, 2020, 12:53:20 PM by heavygunner »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: Push enemy on collision
« Reply #1 on: April 04, 2020, 03:45:10 AM »
Hi.
How are the objects moved?

heavygunner

  • Sr. Member
  • ****
  • Posts: 344
Re: Push enemy on collision
« Reply #2 on: April 04, 2020, 04:07:28 AM »
Hello !

Goal of game is Get the star and reach checkpoint. If Player got Star, AI can chase the player and get it by Collide with him. Same happens vice versa.

I did it. problem is when i hit AI and got star, still I'm in-collision with eemy. So, star is pickedup by player and AI rapidly. So, to avoid that I wanted to push the AI once I got star from him.

I am trying to make similar game like this. But, with Stars. Pls check this gameplay video for reference.

Skip to 0:34

Thanks

heavygunner

  • Sr. Member
  • ****
  • Posts: 344
Re: Push enemy on collision
« Reply #3 on: April 04, 2020, 05:27:05 PM »
I found below code on Unity Answers. I managed to push enemy. But, not in opposite direction. This code telling to Calculate Angle Between the collision point and the player. I have no idea how to do it. But, I tried something.

Code: [Select]
void OnCollisionEnter(Collision c)
 {
     // force is how forcefully we will push the player away from the enemy.
     float force = 3;
 
     // If the object we hit is the enemy
     if (c.gameObject.tag == "enemy")
     {
         // Calculate Angle Between the collision point and the player
         Vector3 dir = c.contacts[0].point - transform.position;
         // We then get the opposite (-Vector3) and normalize it
         dir = -dir.normalized;
         // And finally we add force in the direction of dir and multiply it by force.
         // This will push back the player
         GetComponent<Rigidbody>().AddForce(dir*force);
     }
 }

I pushing the enemy on Collision. But, only in one direction. Not in opposite direction.
Pls check video

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: Push enemy on collision
« Reply #4 on: April 05, 2020, 06:51:29 AM »
Hi.
on the video you have force set to a value, so it will always do that value.
also position is getting a rotation

for you sample you need to get contact point of target, then get player position.
and do contact point - (minus) player position

Then to set opposite use vector 3 and multipy x/y/z by -1.
then use a 'vector 3 normalize' action.

Then use the result to multiply force value (use a vector 3 variable for force variables. set for example to 100,0,100)
then use the result to add the force



heavygunner

  • Sr. Member
  • ****
  • Posts: 344
Re: Push enemy to opposite direction on collision
« Reply #5 on: April 05, 2020, 12:53:02 PM »
Thanks djaydino  ;)
It worked  8)

Qbanyto

  • Full Member
  • ***
  • Posts: 137
  • Vista Larga, Paso corto
Ecosystem this? - Re: [SOLVED] Push enemy to opposite direction on collision
« Reply #6 on: December 27, 2020, 04:23:22 PM »
Can someone make this behavior into a custom action? There is one in the ecosystem now but does work like this. I will ty to implement this manually but if someone can get this as a custom action, this will be VERY helpful for many people :p.

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Re: [SOLVED] Push enemy to opposite direction on collision
« Reply #7 on: December 29, 2020, 04:08:50 PM »
Wow It looks like a fun game!
Have I said how much I love playmaker!!! This is one amazing tool