C
C#2mo ago
KingBarou13!

Slope Physics not working when trying to make Sonic face movement direction

I'm working on a 3D sonic framework in Unity, and I'm starting to implement animations and player rotation. I ran into a bit of a problem where animations are all working as intended which is good, and the rotation works well on flat ground, but it messes up the slope physics completely, and when braking sonic turns at a slight angle for some reason. It should be in the video but it's kinda hard to pick out because I haven't added the braking animations yet. (turning off the rotation script makes everything work normally again so it's definitely the rotation script that's the problem). I've been trying to figure out a solution for a bit now but with no luck, so some help in figuring this out would be greatly appreciated. Here are the relevant scripts for this: RotationHandler (attached to capsule): https://pastecode.io/s/rv1418xi MoveAction: https://pastecode.io/s/8092tzsm PlayerPhysics: https://pastecode.io/s/p2qb2q81
Player Rotation Handler in Unity (rv1418xi) - PasteCode.io
This C# script for Unity manages the rotation of a player character based on movement input. If the player is braking, the rotation is ignored. Otherwise, it calculates the target rotation from the movement vector and smoothly applies it to the player transform, enhancing the gameplay experience.
18 Replies
Shazanman
Shazanman2mo ago
i'd advice to read on how sonic slope physics work https://info.sonicretro.org/SPG:Slope_Physics
Shazanman
Shazanman2mo ago
also i'd advice against rotating the player directly looking at your code
KingBarou13!
KingBarou13!2mo ago
This is the basis of what I've been working with, but it doesn't translate that well into 3D in some parts. Slope detection works, it's just that I'm trying to implement a rotation script for sonic the face in the direction he's moving like face left when going left etc and that new script is what's messing up the slope physics
Shazanman
Shazanman2mo ago
yeah you shouldn't directly rotate the model you should ideally applying a tangential force based on the angle you are on the ramp it's a bit tricky tbh i haven't done this for 3d i have done this for 2d
Shazanman
Shazanman2mo ago
Developer Forum | Roblox
Sonic Slope Tilt & Momentum
I’ve spent the past few months working on creating a Sonic game with a friend of mine and my movement system is practically complete. It involves multiple stages of speed with increases to WalkSpeed over time and ties into several other systems I’ve completed (Many of which use AlignPosition and do not alter WalkSpeed). The last thing that I was...
Shazanman
Shazanman2mo ago
maybe you can get ideas from this ideally you want to emulate SA/2 physics i suppose?
KingBarou13!
KingBarou13!2mo ago
Yeah that's the goal I even pseudocoded a basic momentum system:
float currentSpeed; // Player's current speed
float currentRotation; // Player's current X-Rotation (Positive means going downhill so increase speed, negative means going uphill, so decrease speed)
float slopeDrag; // Deceleration rate going uphill
float slopeAssistance; // Acceleration rate going downhill

if (isGrounded)
{
if (currentRotation < 0)
{
currentSpeed += currentRotation * slopeDrag;
}
else if (currentRotation > 0)
{
currentSpeed += currentRotation * slopeAssistance;
}
}
float currentSpeed; // Player's current speed
float currentRotation; // Player's current X-Rotation (Positive means going downhill so increase speed, negative means going uphill, so decrease speed)
float slopeDrag; // Deceleration rate going uphill
float slopeAssistance; // Acceleration rate going downhill

if (isGrounded)
{
if (currentRotation < 0)
{
currentSpeed += currentRotation * slopeDrag;
}
else if (currentRotation > 0)
{
currentSpeed += currentRotation * slopeAssistance;
}
}
Shazanman
Shazanman2mo ago
tbh even sonic frontiers doesnt have slope physics because it has boost think like a high speed car going on a ramp
KingBarou13!
KingBarou13!2mo ago
Yh i think i get what you mean
Shazanman
Shazanman2mo ago
yeah experiment but you dont rotate the sonic model when you are upward the slope
KingBarou13!
KingBarou13!2mo ago
This is how things look without the rotation handler script. Rotation handler only handles his y-axis rotation so looking left and right
Shazanman
Shazanman2mo ago
that what is causing the issue yeah that is buggy but i wonder can you reduce the cylinders size?
KingBarou13!
KingBarou13!2mo ago
it's cause of the snap function. It's supposed make it so that uneven ground isn't that much of an issue
Shazanman
Shazanman2mo ago
this return doesnt look correct
No description
Shazanman
Shazanman2mo ago
i think you should do something instead then return
KingBarou13!
KingBarou13!2mo ago
sorry but wdym? This is my first time implementing physics like this i only know the basics
Shazanman
Shazanman2mo ago
yeah im also not sure about this until i debug in unity maybe you can try debugging in unity sadly i dont want to download unity again
KingBarou13!
KingBarou13!2mo ago
ohhh alright i was literally about to say that my current project is public on github and if you wanted you could have a look urself lol I've tried a bit before but I'll try again Debug.Log about to work overtime
Want results from more Discord servers?
Add your server