P
P
PsyDuckbest2021-08-29 18:19:40
C++ / C#
PsyDuckbest, 2021-08-29 18:19:40

How to repel an object on impact?

The game is based on a 3D engine, but it’s like two-dimensional, the characters are sprites, so they don’t “look” anywhere, because of this I don’t quite understand how to repel an enemy when hit or a character when receiving damage. I tried to take the position of the player and the enemy, but did not figure out how to push one away from the other with these parameters, can you tell me?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BFGunner, 2021-08-29
@BFGunner

Add box2D or other colliders to objects.
Then write a script in which there will be void OnTriggerEnter2D () or OnCollisionEnter2D ()
That is, this script will check the collision of the object on which the script is hung with another object that has a collider. And in this input you specify what to do with the object on which the script is hung. Well, or prescribe the interaction of the collided object.
The simplest is to take a vector (Object Position 1 - Object Position 2) and use AddForce with a negative sign (Don't forget to assign a RigidBody to the objects). Well, or register a damped movement exclusively TransformPosition.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question