Sure, I understand you need a script to control the rotation of the legs of a character based on its speed. We will assume that the character has a Rigidbody and a CharacterController component.
Let's proceed with writing the script.
Before jumping into the script, make sure you have the following setup in your Unity scene:
- A GameObject with a Rigidbody and CharacterController attached.
- Two legs, each composed of two parts: upper leg (thigh) and lower leg (shin).
- The script assumes there are empty GameObjects representing the rotation point (hip for upper leg, knee for lower leg) configured as the parents of the corresponding parts of the leg.
Here's a script to achieve that:
LegController.cs
using UnityEngine;
public class LegController : MonoBehaviour
{
public Rigidbody rb;
public Transform upperLegLeft, lowerLegLeft;
public Transform upperLegRight, lowerLegRight;
public float speedThreshold = 0.1f; // Speed below which the legs go to idle position.
public float maxLegSwing = 30f; // Maximum rotation angle for the legs' swing.
public float smoothSpeed = 5f; // Speed at which the legs move smoothly.
private float currentSpeed;
private Vector3 initialUpperLegLeftRotation, initialLowerLegLeftRotation;
private Vector3 initialUpperLegRightRotation, initialLowerLegRightRotation;
void Start()
{
// Store initial rotations for returning to idle position
initialUpperLegLeftRotation = upperLegLeft.localEulerAngles;
initialLowerLegLeftRotation = lowerLegLeft.localEulerAngles;
initialUpperLegRightRotation = upperLegRight.localEulerAngles;
initialLowerLegRightRotation = lowerLegRight.localEulerAngles;
}
void Update()
{
// Calculate the character's speed
currentSpeed = rb.velocity.magnitude;
// Calculate the swing angle based on speed
float swingAngle = Mathf.Lerp(0, maxLegSwing, currentSpeed / speedThreshold);
swingAngle = Mathf.Clamp(swingAngle, 0, maxLegSwing);
// Interpolate between idle and moving positions
if (currentSpeed > speedThreshold)
{
RotateLegs(swingAngle);
}
else
{
// Smoothly return to initial position
SmoothReturnToIdle();
}
}
private void RotateLegs(float swingAngle)
{
float angle = Mathf.PingPong(Time.time * smoothSpeed, swingAngle) - (swingAngle / 2);
// Rotate upper legs
upperLegLeft.localRotation = Quaternion.Euler(initialUpperLegLeftRotation.x, initialUpperLegLeftRotation.y, initialUpperLegLeftRotation.z + angle);
upperLegRight.localRotation = Quaternion.Euler(initialUpperLegRightRotation.x, initialUpperLegRightRotation.y, initialUpperLegRightRotation.z - angle);
// Rotate lower legs
lowerLegLeft.localRotation = Quaternion.Euler(initialLowerLegLeftRotation.x, initialLowerLegLeftRotation.y, initialLowerLegLeftRotation.z + (angle * 0.5f));
lowerLegRight.localRotation = Quaternion.Euler(initialLowerLegRightRotation.x, initialLowerLegRightRotation.y, initialLowerLegRightRotation.z - (angle * 0.5f));
}
private void SmoothReturnToIdle()
{
upperLegLeft.localRotation = Quaternion.Lerp(upperLegLeft.localRotation, Quaternion.Euler(initialUpperLegLeftRotation), Time.deltaTime * smoothSpeed);
lowerLegLeft.localRotation = Quaternion.Lerp(lowerLegLeft.localRotation, Quaternion.Euler(initialLowerLegLeftRotation), Time.deltaTime * smoothSpeed);
upperLegRight.localRotation = Quaternion.Lerp(upperLegRight.localRotation, Quaternion.Euler(initialUpperLegRightRotation), Time.deltaTime * smoothSpeed);
lowerLegRight.localRotation = Quaternion.Lerp(lowerLegRight.localRotation, Quaternion.Euler(initialLowerLegRightRotation), Time.deltaTime * smoothSpeed);
}
}
Instructions:
- Attach the LegController script to a GameObject (typically the parent object of the character).
- Assign the Rigidbody to the
rb field in the Inspector.
- Assign the correct child transforms for the left and right legs (upper and lower parts).
Now, the script will control the rotation of the legs based on the speed of the character. When the speed is low, the legs will smoothly transition to an idle position. When the character is moving, the legs will rotate to simulate walking.
This script uses Mathf.PingPong to create a back-and-forth motion for the legs based on Time.time. The rotation is smoothed to create a natural movement style, similar to that of a human girl's walking motion.