How to align 2d object to terrain?
I am trying to make a 2d snowboarding game (similar to alto's adventure or ski safari) and can't figure out how to adjust the rotation of the player object based on the terrain. Any help is appreciated! (Oh, and preferably in c# since I am not familiar with javascript).
Answer by Menyus777 · Jul 11, 2017 at 09:58 PM
Its easy, TerrainData.GetSteepness https://docs.unity3d.com/ScriptReference/TerrainData.GetSteepness.html
You check the Derivate at every point, but i wonder why you use terrain in a 2d game
Appreciate it, man! The thing is, the slope is curvy, so how can I check the approximate angle? Thanks for the effort!
Derivate :P
or or or if u really really hate that, cut your cruvy slope into 3 pieces, with 3 different angle value, like in modelling perfect sphere does not exists it is just from small polygons the more u have the more detailed sphere u get :), so for more detailed curvy slope, cut your slope into more components. For grahpics: Do a black line which is the collider and the slope, that the player ski on, and then in front of it hide it with the detailed sprite, the "real" slope
Answer by iLucas13 · Jul 12, 2017 at 12:07 AM
@Menyus777 Maybe terrain would be the wrong term, it's a sprite with a collider, will this method still do the trick?
No because a sprite doesnt inherit a terrainData so the solutions are the following: - You need to check the derivate of the sprite, this might sound complicated but actually you can learn how to derivate in like 10 $$anonymous$$utes if you dont know how to do it - or if you still have problems because math can be a pain in the ass sometimes, you can make your sprites logically, so u know that this "Long_Icy_Ramp" is always 45 degree
And to get which steepness u need, u make an Update() function(it runs evry rendered frame) which casts a raycast down and reads out which sprite the player is above, then u read out that sprite steepness and rotate your player accordingly
Btw arent you are that pianist from youtube Lucas $$anonymous$$ing? :D
@$$anonymous$$enyus777 Sadly, no, LOL, but here's a clip of me playing the guitar with my band on youtube: https://www.youtube.com/watch?v=hW9qA2jBkcg (we're pretty big in the gospel scenario in Brazil, the lyrics are in Portuguese but you still may be able to enjoy the music, it's pretty good!)
Enough nonsense.
I was able to raycast from the bounds of my player's collider. But the issue now is that when the player rotates, the rays are still cast from the same position, even though I set up a UpdateRaycastOrigins method.
Damn bro nice music u made there, ill provide you a small drwaing how i would solve your issue.
Your answer
Follow this Question
Related Questions
I was trying to make my game in unity but I got stuck while making "game over" part. please help! 0 Answers
2d Grid Based Movement, Won't Work 0 Answers
2D AI movement 0 Answers
C# delaying command 0 Answers