O
O
okamilov2016-07-21 13:15:53
Unity
okamilov, 2016-07-21 13:15:53

How to rotate the sprite in the direction of physical movement. body?

c5672cb04a244a038ae4a61019bee12f.png
Hello!
Such a problem - there is a laser, there is a wall. It was necessary to make a ricochet. After trying many options, I settled on this: just give power to GetComponent (). AddRelativeForce and leave the unit to calculate itself.
Everything works fine, but an unpleasant problem arose, namely, that I can’t find the motion vector of the body (I wanted to expand the sprite after the collision along the motion vector)
And the question is how to expand the sprite in the direction of physical movement. body?
PS - just in case, I'll write problems with other methods, maybe there will be easier solutions
1) I move the body (or rather teleport it?) with the transform.Translate command, and in case of collision with the transform.Rotate command, I turn it to the desired angle. But the following problem arose, imposing a box collider on a beam f-I worked the same in width and length (which is of course an error), and by making the collider a line, my laser often teleported through such a collider. I also tried several box colliders, but again due to transform.Translate (even reducing the teleportation distance per second) there were bugs
2) I wanted to just force the object (GetComponent().AddRelativeForce), in the hope that there would be no problems of passing a linear collider or reacting to two box colliders that are close. Turn as before - code. Everything almost worked - a normal ricochet, the sprite unfolds, but the body - no, it just went right through the collider. I have been looking for a command for a long time how to deploy such a body or somehow give strength to turn, but ...
3) Here I decided to leave the body ricochet to UNITY physics, and deploy the sprite with my function when the beam collides with my GM on the laser, which contains a trigger collider (even in another Layer), but with different variations, some bugs came out
4 ... 5 ... 6 - a couple more ideas (for example, in case of collision, check the location of the laser relative to the beam and apply the appropriate function - for width or length) with similar problems

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2016-07-21
@okamilov

You can use Rigidbody2D.velocity to find out the direction of movement, then rotate the sprite to the desired direction. To prevent the laser from piercing other colliders, change Rigidbody2D.collisionDetectionMode to CollisionDetectionMode2D.Continuous .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question