Gradually rotate to slope angle
In my platformer game, I want my character to rotate to the angle of the slope they are standing on. However, to make this work properly I dont want them to suddenly rotate to the angle upon making contact with the slope else it will look horrible and undoubtedly throw up a tonne of collision issues. Instead, I want them to gradually rotate as shown in the images, so that they will rotate as much as they can each frame until they cant any further due to a collision with the back corner. How would I do this, and how would I change the pivot point of the character to the front bottom corner in order to make this work? Code in C#. Thanks.
Answer by stepan-stulov · Aug 18, 2015 at 01:49 PM
You don't have to have visual and collision representations of the level be the same. What you can always do is a sharp corner in the visual representation and a smooth round (or at least a polygon with more steps to approximate roundness) collider int the collision representation.
Im not doing it for aesthetics, its because instantly rotating it to slope angle will put it through the floor if its at the bottom of a slope, which completely messes it up
Answer by Owen-Reynolds · Aug 18, 2015 at 02:01 PM
Do you really need the precise placement shown in the pictures to the right? Most games let there be a little overlap -- your feet are in the grass, or bushes, or soft dirt ... . If you need that picture it seems like more of a trig problem (it won't slowly rotate -- it will snap to the computed math rotation as it moves, which will slowly change.)
But most games, when you hit the slope, will slowly spin, will put your back foot through the ground, but you've probably moved by then anyway. You can look up finding the normal and gradual rotations.
The reason I decided to do it how I said was because having a snap-to rotation put the character into the floor as soon as they touched the bottom of the slope, causing lots of collision problems. The main reason Im having trouble with this is because I dont know what to do when it comes to the base and top of a slope.
But, what part do you need and what part is merely "I feel like this might work?"
When the player is fully on a sloped ramp, must exactly match the slope? And then just do whatever works when two slopes meet? Is this physics-based, or hand-moved? Physics-based give you the option for spring-joints to fix the problem, but they tend to give you 90% of what you wanted.
It feels like it's still at the design problem stage.
The other option is just rotating the graphics ins$$anonymous$$d of the actual player. However, something that I do want is for the player to jump at the angle of the normal of the slope ins$$anonymous$$d of straight up, and I thought this was easiest to implement if the player was already rotated to the slope so that the jump direction would just be transform.up
Answer by Saddamjit_Singh · Sep 04, 2017 at 05:45 AM
Hi ,
Use the script below -
RaycastHit hit;
Quaternion rot;
public int smooth;
void Update()
{
Ray ray = new Ray(transform.position, -transform.up);
if (Physics.Raycast(ray, out hit))
{
rot = Quaternion.FromToRotation(transform.up, hit.normal) * Quaternion.Euler(0, transform.rotation.y, transform.rotation.z);
transform.rotation = Quaternion.Lerp(transform.rotation, rot, Time.deltaTime * smooth);
}
}
For advice for just one person, try to check the date. This Q is two years old. Almost no one cleans up a Q (for example, writing that they found an answer.) Any Q more than about a month old, they're done with it.
It is O$$anonymous$$, even encouraged, to add better answers for "the internet" to old Qs. But do a Search first. What you wrote here is already a common, easy-to-find answer. There's also another easy-to-find answer using several ground contacts (which is what the original poster seemed to want.)
If it's a fresh question with an easy-to-find answer here in UA, it's considered polite to just make a link, ins$$anonymous$$d of a cut/paste.