Trying to simulate roll of character's body down a luge slide
Hi folks! I've got this pretty close to the way I want it. The object is for this penguin to slide down a luge course and roll left or right with the turns. The setup is like this: I have a rigidbody go with a capsule collider shaped for the character (gunther_rb_parent). The luge course and character's physics material have no friction and no drag to simulate ice. gunther_rb_parent has the model of the actual character attached as a child. gunther_rb_parent has a configurable joint on it to lock local rotation on the Y and Z axis.
The gunther model has a calculated rotation applied to his y rotation based on his change in Z rotation. This is the attempt to make the character roll based on how much he turns.
The code has boiled down to something very simple now that I'm only affecting the child (gunther) of the rigidbody (gunther_rb_parent) which has no physics.
float lastAngY = 0f;
public bool useExtraRotation = false;
float lastAngZ = 0f;
bool isAngleZSet = false;
[Range(-1f,1f)]
public float angZFactor = 1f;
void FixedUpdate()
{
if (Input.GetKey(KeyCode.Space))
{
rb.AddForce(-gameObject.transform.up * force);
}
if (!useExtraRotation)
return;
float eulerZ = gunther.transform.eulerAngles.z * angZFactor; //angZFact = -1f to 1f
float yAddAng = 0f;
if (!isAngleZSet)
{
isAngleZSet = true;
lastAngZ = eulerZ;
}
float dAngZ = eulerZ - lastAngZ;
lastAngZ = eulerZ;
yAddAng = Mathf.Clamp(dAngZ , -10f, 10f);
AddToLast5Rot(yAddAng);
gunther.transform.Rotate(Vector3.up, yAddAng, Space.Self);
}
OK. So the problem that remains is that I really only want the character to turn when in a turn then on a straghtaway I want him to try to face back forward. I can't wrap my brain around the math for this and would love some help! A video of this system in action can be seen here:
http://naplandgames.com/images/forum_posts/gunther_luge_testing.mp4
Thanks!!
Your answer
Follow this Question
Related Questions
Get object to rotate freely influenced by gravity 0 Answers
Use Mobile Joystick with Unity 3d RigidBody 1 Answer
Rotate Rigidbody on Y Axis based on Velocity on X and Z axis 3 Answers
stack-type game problem (blocks fall). bloques se caen en juego de apilar cajas 0 Answers
Rotate Rigidbody towards target rotation using AddTorque() 0 Answers