Hi, I’m really struggling with rotating my rigidbody character’s forward to the movement forward, while also being able to run through a loop de loop upside down. I’ve tried a bunch of different variations of Quaternion.LookRotation, transform.LookAt, transform.RotateAround etc to not much luck. Currently I’ve just got it rotating towards the InputDirection since trying to make it face it’s velocity just caused in to constantly spin. The grey debug line is the forward direction. The rotation on the rigidbody is locked so it won’t fall over from the force.
Firstly, if you’re using a rigidbody, then you want to rotate it via the rigidbody and not the transform component.
Then to rotate it towards your velocity heading, accounting for ‘up’, you take your current heading and the heading of your velocity, get the rotation between the two (Quaternion.FromToRotation), work out your final rotation, and then rotate towards that.
You can run the delta value forQuaternion.RotateTowards through an animation curve to smooth out the rotation as well.
It works best when you maintain your own Vector3 for the players current heading, or potentially what the current ‘up’ value is, and update via the described method.
Hey, thanks for the tip about the rigidbody/transform rotation. As for the other bit, could you extrapolate on what you mean by heading? So far I’ve tried using Rigidbody.transform.forward.normalised which somewhat works but is very jittery and leans slightly, the MovementDirection itself, the raycast normal, the previous velocity etc and I’m kind of at a loss. Also not too sure if maybe my camera control is affecting its effectiveness…
Thanks!
The rotate towards probably just needs to account for time. Ergo, someMaxDelta = someValue * Time.deltaTime;, alongside the advice above.
As for the heading, it depends on the project. The last time I did something like this, it was to orient the camera for a ball-rolling puzzle game, which included variable gravity. The camera would need to orient towards ‘up’ correctly when it changed. So in that case, the ‘heading’ was the current direction of gravity.
Presuming you’re making some kind of Sonic game, and have some means to determine what ‘up’ is, then I think you’ll want to use a direction that is perpendicular to ‘up’, pointing inline with the rigidbody’s velocity. This is where a double Vector3.Cross comes in.
For example:
Vector3 up = GetUpDirection();
Vector3 forwards = rigidbody.velocity.normalized;
Vector3 crossRight = Vector3.Cross(up, forwards);
// this points inline with velocity, prependicular to 'up'
Vector3 heading = Vector3.Cross(crossRight, up);
Though a Sonic style game needs a REALLY good understanding of how to use vectors and quaternions, probably more than my current understanding quite frankly.
As for the rotation, still only half luck, It has it’s proper delta now but no dice. Either the heading and the velocity heading are the exact same, or I swap the forwards to the rigidbodies forward which kinda sorta works minus constantly leaning and overshooting, adding the forward to the heading half works but is very slow and not fully synced, etc. Been using my raycast normal and my transforms up as up which both seem to somewhat work?
Had one of my friends who is more math inclined look over it and help me with it for a few hours but they didn’t get too far either with it, so maybe I should try out MovePosition or a kinematic controller. As a sidenote, do you know any good programs out there to visualize this sort of stuff?
Thanks for your continued help
… have you tried… Unity3D? That’s what Spiney is doing with the OnDrawGizmos() stuff above. You can really go nuts with that.
Blending multiple axes of motion like this, eg, being able to look in any direction as you go around a loop, is always brain-bending because of the shifting coordinate systems involved. Sometimes parenting objects below each other is an answer, but other times that may be unsuitable, depending on what you’re trying to do exactly.
One way to make it easier is to make some tooling to pass your player through the loop based on one set of controls, eg, make your Z / X buttons go forward and backward through the loop path, letting you iterate and test your vectors at each point in space.
You might find some insight from the approaches used for walking on a spherical world, for which there are many tutorials out there, such as those by Sebastian Lague.
Was kinda wanting to know if there was something like mathlab or something for easy visualization, but doing it in engine makes works just as well.
Meanwhile, for insights, I’ve dug through at least a dozen other third person/gravity based projects, turns out most of them just sidestep it and rotate a player model instead while keeping the rigidbody only rotating towards the ground. Not quite what I wanted, but I guess it’ll be easier and cause less headaches in the long run.