- Home /
How to make a ragdoll not rotate but still be non kinematic?
I have made a video describing the problems.
https://www.youtube.com/watch?v=xxWD_9d0Jnc&feature=youtu.be link text
I want to make a character system, composed by a ragdoll that can move, but doesnt act as a ragdoll, only the collisions od the bones, I prefer it to be non kinematic.
Is there some way to stop the rotation of the ragdoll, but still allow the movement? There is also another problem. In my script there is some lines that say:
ragdoll = transform.FindChild("Armature") as Transform;
rb = ragdoll.GetComponentInChildren(typeof(Rigidbody)) as Rigidbody;
TP_Controller.rb.velocity = MoveVector;
This lines mean that all of the rigidbodies childs move with a movevector velocity. The problem is that all the bone colliders move at the same time. I would like to create a single bone (like the middle spine), to move and the rest of the armature ragdoll follows the movement, because if I cancel the rotation of every rigidbody of the ragdoll, each bone moves at its own direction, they dont act as a whole.
Any ideas? I will take any idea in consideration, ideas are very important for me, since I dont know what to do. So I will get this answer by myself since the unity community people dont help very much this days.
Here are the scripts:
Dont pay attention that they are too long, they only move the character sideways and vertically, and also applies gravity and slide, they move with a movevector Vector3 applied in worldspace.
To test this script you must have a ragdoll with the armature named Armature.
TP MOTOR
using UnityEngine; using System.Collections;
public class TP_Motor : MonoBehaviour {
public static TP_Motor Instance;
public float MoveSpeed = 20f;
public float Gravity = 10f;
public float TerminalVelocity = 20f;
public Vector3 MoveVector { get; set; }
public float VerticalVelocity { get; set; }
void Awake() // Debe quedarse en awake
{
Instance = this;
}
public void UpdateMotor() // Debe quedarse en update motor
{
ProcessMotion();
}
void ProcessMotion()
{
// Transform MoveVector to World Space
MoveVector = transform.TransformDirection(MoveVector);
// Normalize MoveVector if Magnitude > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// Multiply MoveVector by MoveSpeed
MoveVector *= MoveSpeed;
// Reapply VerticalVelocity MoveVector.y
if (!TP_Controller.Instance.isgrounded == true)
MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);
// Apply Gravity
ApplyGravity();
// Move Character in World Space
TP_Controller.rb.velocity = MoveVector;
}
void ApplyGravity()
{
if (TP_Controller.Instance.isgrounded == true)
return;
if (MoveVector.y < -TerminalVelocity)
MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
if ((TP_Controller.Instance.isgrounded == false) && MoveVector.y > -1)
MoveVector = new Vector3(MoveVector.x, -10, MoveVector.z);
}
}
TP CONTROLLER
using UnityEngine;
using System.Collections;
public class TP_Controller : MonoBehaviour
{
public static TP_Controller Instance;
public static Rigidbody rb;
public static Transform ragdoll;
RaycastHit hit;
public bool isgrounded;
}
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>();
rb.isKinematic = false;
}
void Update ()
{
if (Camera.mainCamera == null)
return;
GetLocomotionInput();
TP_Motor.Instance.UpdateMotor();
IsGrounded();
}
void IsGrounded()
{
RaycastHit hitInfo;
if (Physics.Raycast(transform.position + Vector3.up * 0.8f, Vector3.down * 0.5f, out hitInfo, Mathf.Infinity))
{
if (hitInfo.distance > 0.1f) // Change .1f to what you need
Debug.DrawRay(transform.position + Vector3.up * 0.05f, Vector3.down * 0.05f, Color.green);
isgrounded = false;
if (hitInfo.distance < 0.1f)
isgrounded = true;
}
}
void GetLocomotionInput()
{
var deadZone = 0.1;
TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
TP_Motor.Instance.MoveVector = Vector3.zero;
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);
}
}
$$anonymous$$aybe I havent explained well the problem, the problem is that the ragdoll crawls like a reptile, and I want it to move like the first part of the video, but as a ragdoll.
Check Character Joints and put some restrictions on them