- Home /
How can I select a ragdoll entirely as a rigidbody in animation
I am trying to make a character controller with a ragdoll. So far I have made the character move in ragdoll state with the arrows keys. The problem is that I want to add the animations, and when I do, the ragdoll becomes kinda static.
The problem I have is I want to add the movement animations. And while the character moves, I want it to have a rigidbody that acts like a general collider. I dont want to use a character controller collider. So far it works when there is no animation, like idle or run.
The main problem arises when I activate an animation. Then all the ragdoll colliders kinda like disspear, and they are not detected, when there is an animation. Why this happens???
This is my code:
public static TP_Controller Instance;
public static Rigidbody rb;
public static Transform ragdoll;
GameObject Piso = GameObject.Find("Piso");
public LayerMask mask;
RaycastHit hit;
public bool isgrounded;
// SIMPLE RAGDOLL
Animator animator;
public static Rigidbody[] rigidbodies;
public static Transform[] rigidTransforms;
Vector3[] lastRigidPositions = new Vector3[1];
Vector3[] rigidVelocities = new Vector3[1];
Vector3[] rigidAngularVelocities = new Vector3[1];
Quaternion[] lastRigidRotations = new Quaternion[1];
[Range(0f, 1f)]
public float veclocityLerp = .2f; // If zero, then no momentum will be transferred to the ragdoll
[Range(0f, 1f)]
public float keepAngularMomentum = .4f; // If zero, then no angular momentum will be transferred to the ragdoll
[Range(0f, 240f)]
public float angularDrag = 30f; // Rigidbodies angular drag. Unitys parameter
[Range(0f, 4f)]
public float drag = .5f; // Rigidbodies drag. Unitys parameter
float maxAngularVelocity = 100f; // Rigidbodies maxAngularVelocity. Unitys parameter
public bool useGravity = false; // Ragdoll is affected by Unitys gravity
public bool ragdolled = false; // Instantiate ragdoll with same pose and momentum as master
public void Awake () // Debe quedarse en awake
{
ragdoll = transform.FindChild("Armature") as Transform;
rb = ragdoll.GetComponentInChildren(typeof(Rigidbody)) as Rigidbody;
Instance = this;
animator = GetComponent<Animator>();
rigidbodies = GetComponentsInChildren<Rigidbody>();
int i = rigidbodies.Length;
System.Array.Resize(ref rigidTransforms, i);
System.Array.Resize(ref lastRigidPositions, i);
System.Array.Resize(ref rigidVelocities, i);
System.Array.Resize(ref lastRigidRotations, i);
System.Array.Resize(ref rigidAngularVelocities, i);
i = 0;
foreach (Rigidbody rigidbody in rigidbodies)
{
rigidTransforms[i] = rigidbody.transform;
i++;
}
i = 0;
foreach (Transform rigidTransform in rigidTransforms) // Set some of the Unity parameters
{
lastRigidPositions[i] = rigidTransform.position;
lastRigidRotations[i] = rigidTransform.rotation;
rigidTransform.rigidbody.useGravity = useGravity;
rigidTransform.rigidbody.angularDrag = angularDrag;
rigidTransform.rigidbody.drag = drag;
rigidTransform.rigidbody.maxAngularVelocity = maxAngularVelocity;
rigidTransform.rigidbody.isKinematic = false;
i++;
}
if (i == 0)
Debug.LogWarning("There are no rigid body components on the ragdoll " + this.name);
}
void Update ()
{
if (Camera.mainCamera == null)
return;
GetLocomotionInput();
TP_Motor.Instance.UpdateMotor();
IsGrounded();
}
void IsGrounded()
{
RaycastHit hitInfo;
if (Physics.Raycast(rb.transform.position + Vector3.down * 3f, Vector3.up * 5f, out hitInfo, Mathf.Infinity, mask))
{
if (hitInfo.distance > 0.1f) // Change .1f to what you need
Debug.DrawRay(rb.transform.position + Vector3.down * 3f, Vector3.up * 5f, Color.blue);
isgrounded = true;
}
if (hitInfo.distance < 0.5f){
Debug.DrawRay(rb.transform.position + Vector3.down * 3f, Vector3.up * 5f, Color.red);
isgrounded = false;
}
}
void GetLocomotionInput()
{
var deadZone = 0.1;
TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
TP_Motor.Instance.MoveVector = Vector3.zero;
if (TP_Controller.Instance.isgrounded != false)
{
if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
}
}
Your answer
Follow this Question
Related Questions
Ragdoll problem with animations 0 Answers
Rigidbody causes ragdoll character to fly 4 Answers
Restrict the rigidbody to be affected by only 1 joint 0 Answers
Rigidbody+collider conflicts with ragdoll structure 3 Answers
How to fix Ethan ragdoll 0 Answers