#2D - Pushing objects away to allow passage

1 messages · Page 1 of 1 (latest)

dim crane
#

https://gyazo.com/6decc03820f75cca2ce8fe0c9cd17311
The ideal would be for it to work like this, right now this is the code:

private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            Vector3 perpVector = Vector3.Cross(rb.velocity, Vector3.forward);
            Vector2 pushDirection = new Vector2(perpVector.x, perpVector.y).normalized;
            Debug.Log("Direction:" + pushDirection);
            
            collision.gameObject.GetComponent<Rigidbody2D>().velocity = pushDirection * pushForce;
        }
    }```
and the object is pushed away in a clockwise direction, if collided from the left side he will be pushed down, etc
#

@kindred fern thread is here, sry for bothering you so much

kindred fern
#

no prob :p

#

anyway, here's my general suggestion:

#

take the direction the player is moving

#

we want to shove everyone away from that line

#
Vector3 dir = rb.velocity;
#

so let's say there's an enemy we're bumping into

#

we should first find the vector from us to the enemy

#
Vector3 enemyDir = enemy.transform.position - transform.position;
enemyDir = enemyDir.normalized;
#

now we have two vectors: moveDir and enemyDir

#

actually, let me rename that

#

The interesting question here is: which direction would move the enemy away from the line that we're moving along?

#

We can find that out by subtracting the part of the enemy's direction that lines up with our own direction

#

i need a visual, hang on

#

The red line is our movement. The blue line is the direction to the enemy.

#

The orange line is the direction we'd like to shove the enemy.

#

oh, actually, this is simpler than I was thinking

#

All we need to do is find the vector between those two endpoints

#

so like

#
var shove = enemyDir.normalized - moveDir.normalized;
shove = shove.normalized;
dim crane
#

EnemyScript
The enemy is always moving towards the player in a normalized direction btw csharp private void FixedUpdate() { Vector3 direction = (player.position - transform.position).normalized; rb.velocity = direction * velocity; }

kindred fern
#

note that, if the enemy is EXACTLY in front of you, this will try to normalize a zero vector

#

so maybe have a special case that does the cross product thing if the difference between moveVec and enemyVec is extremely small

dim crane
#

so shove would basically be the pushDirection

kindred fern
#

yeah

kindred fern
#

I guess you'd subtract that from the enemy vector and normalize it

dim crane
#

i dont think i understood the visual tho, so red/player would move in the direction of the pointer and lets say that it collided with the Enemy Object on the pixel where the orange line starts, it would then be shoved away

kindred fern
#

The enemy is at the black X

#

which got covered up by the arrows, oops

dim crane
#

all lines are moving in the direction of the pointers right? how does the player collide with the enemy in the visual, since the enemy is always chasing in the direction of the player

kindred fern
#

the red line is the motion of the player

#

the blue line is the direction to the enemy

#

the enemy's size is not relevant

#

you just do this whenever you collide with them

#

for a visual, put your two arms out, then imagine a line drawn from your right finger to your left finger

#

your right hand is the direction you're moving and the left hand is the direction to the enemy

kindred fern
dim crane
#

so for example, the player is going in the direction of the red line while the enemy is going in his direction after him from the black X which is the left side of the player basically, when the enemy collided with the players left side for example at the end of the red pointer, he would be shoved back according to the orange pointer

kindred fern
#

the enemy's movement is irrelevant

#

all you care about is that you're touching them

#

in this case, the enemy is to the right of (and a little above) the player

#

the player would be at the origin of the red and blue lines

kindred fern
dim crane
kindred fern
#

If you want it to be less extreme, you could blend it with the vector from the player to the enemy

#

So that they don't get pushed 100% aside

#

a 50-50 split would make a direct hit shove them to the side at a 45 degree angle

dim crane
#

imma switch this to the actual project to see better how its working instead of just agaisnt 1 squaer

#

oh can't do that yet, cause FixedUpdate on Enemy is still ruining it

kindred fern
#

I would suggest making enemies lerp their current velocity towards their desired velcoity

#

er, not lerp

#

just use Vector3.MoveTowards

#

e.g. Vector3.MoveTowards(rb.velocity, desiredVelocity, Time.fixedDeltaTime);

#

this will accelerate them at 1 meter per second

dim crane
kindred fern
#

You should not mess with the position directly.

#

I mean to use MoveTowards to adjust the velocity of the enemy over time

#

so that they accelerate over time

kindred fern
#

where speed and acceleration are fields on the enemy

#

when you push an enemy, you can give it a decently large speed, so that it flies away

#

it'll have to slow down and start moving towards you again

dim crane
#

so i should set acceleration to like a float 0.1f and after 10 frames their speed would be back to normal?

kindred fern
#

if the acceleration is 1f, they will accelerate by one meter per second, per second

#

so after 3 seconds, they'll get up to 3 meters per second

#

(or until they hit their top speed)

dim crane
kindred fern
#

ah, that's because they can collide with each other, isn't it

#

What shape is the enemy collider?

#

Using a circle collider would help, if those are boxes

dim crane
#

yep

kindred fern
#

yeah, the boxes made them lock together

dim crane
#

I guess thats all for this then

#

Im really thankful for your help, without it I would have probably lost many hours trying to figure this out

kindred fern
#

no problem!

#

Working with vectors is very common

#

so it's good to get familiar with them

kindred fern
dim crane
#

I have a problem when learning new things that I basically can't wrap my head around it and then months later out of nowhere, my brain just clicks and im like, wait a minute, it all makes sense now

kindred fern
#

pointing your hands in different directions and imagining the vectors can be very useful :p

dim crane
#

Can also make me look like a psychopath depending on where im doing it

#

Anyway, thanks again, should i close the thread