- Home /
Torque with raycast
I have a 2D game where you mine asteroids with a laser. I have accomplished this using a raycast with a line renderer. My trouble comes when adding torque to the asteroid. I want the asteroid to spin if it is hit at an angle but I can't seem to figure out an equation for determining rotation direction (clockwise or counter-clockwise) and an equation that will then give the appropriate amount of torque for the offset. Thanks for the help.
Answer by thirtiesareoldies · Jun 04, 2020 at 11:45 PM
Solved. Super simple. You get the equation for the line between your player your object using y=mx+b and the 2 positions. Then find out if the Y-value of your hit point is above or below the Y-value for your line when the X-value of your hitpoint is plugged into your line equation. That will tell you if your raycast hit to the left or right of center and whether it should spin clockwise or counter-clockwise.
Your answer
Follow this Question
Related Questions
Collision 2d is not working perfectly 0 Answers
OnTriggerEnter2D triggering multiple times when moving around inside the trigger area. 1 Answer
Set ContactFilter2d to not use child object's triggers. 2 Answers
Character Controller 2D physics or raycast ?? 1 Answer
PhysicsMaterial2D bounces back in a strange angle when increasing bounciness 1 Answer