- Home /
How to align a sprite to a slope without using fixed angle?
Hello Unity Community!
The title is a bit weird, I know. Here's what I'm trying to achieve:
I want my sprite to angle itself when I go up a slope, like shown in the screenshot. However, disabling fixed angle will mess up my movement, because the character will spin in an undesirable way. The only solution I've come up with so far, is detecting when the character collides with a slope and rotating it, but I have the feeling there should be a better way. Can the physics engine handle this for me?
Thank you for your time!
Answer by AngryBurritoCoder · Jul 04, 2015 at 09:16 PM
You can raycast down your character and rotate the player in the direction of the normal of the hit point
I'm not that good at using raycasts. Could you provide a short example for your solution?
you need a raycast on the front and one on the back or the model will enter the ground.
@fafase: no, i already have colliders on both the character and the ground tiles, no raycasts needed there.
asically you raycast down from center of character , if you hit the ground , get its hit point and then get its normal ( I assume you know what normal is) , then you can rotate the whole character so that the updirection of the character is aligned with the normal. Use http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
I would write some code, but its late Nd I'm on mobile
Your answer
Follow this Question
Related Questions
Why Rigidbody 2d doesnt stop sliding? 2 Answers
Bumper physics not working, 1 Answer
Player 2D getting stuck while moving 2 Answers
physics Material 2D not working !!!! 1 Answer
While Moving Left or Right my character falls more slowly. 2 Answers