- Home /
How do I stop a Ragdoll passing through fast moving colliders?
G'day all,
I posted this question on the Forum a few days ago but I think it is more suited to "Answers"...
I am having a little issue with my game (I use the term loosely at the moment) I have a Ragdoll character which I move, snapping to a grid, locked via the Root Joint to the Y axis... It is still pretty rough but I am getting there. The idea is to (eventually) move the little guy to avoid oncoming obstacles moving at ever increasing speeds. Pretty simple thus far but my issue at the moment is that if the approaching obstacle/GameObject is moving with any speed my Ragdoll will pass strait through it and not collect/hit/impact it...
I have attached a short video(Added as .zip) to help illustrate what I'm talking about (I am manually controlling the approaching cube...) and the 2 scripts that are on the player. Also, I have checked that all colliders are not set to Triggers, Rigidbodies are not Kinematic and "Use Gravity" is not selected.
Any help would be greatly appreciated. PS: kinda new to C# so please forgive me if my code looks odd and a little messy.
---My movement code. soon to include "Swipe".
using UnityEngine;
using System.Collections;
public class PlayerSwipeMovement : MonoBehaviour {
public float moveTime = 0.1f;
//private Rigidbody rBody;
private float inverseMoveTime;
private Vector2 touchOrigin = -Vector2.one;
public int timer = 0;
public Component[] rgdBodys;
private float angDrag;
private float oldDrag;
void Start()
{
inverseMoveTime = 1f / moveTime;
rgdBodys = GetComponentsInChildren<Rigidbody>();
}
void Move(int xDir, int zDir)
{
if(rgdBodys.Length == 0)
{
rgdBodys = GetComponentsInChildren<Rigidbody>();
}
if(xDir > 0)
xDir = 1;
if(xDir < 0)
xDir = - 1;
if(zDir > 0)
zDir = 1;
if(zDir < 0)
zDir = - 1;
float newX = transform.position.x + xDir;
float newZ = transform.position.z + zDir;
Vector3 start = new Vector3(transform.position.x, 0, transform.position.z);
Vector3 end = new Vector3(newX, 0, newZ);
foreach(Rigidbody rBody in rgdBodys)
{
angDrag = rBody.GetComponent<Rigidbody>().angularDrag;
oldDrag = rBody.GetComponent<Rigidbody>().drag;
rBody.angularDrag = 1.0f;
rBody.drag = 3.0f;
rBody.MovePosition(end);
if(rBody.position == end)
{
rBody.drag = oldDrag;
rBody.angularDrag = angDrag;
}
}
return;
}
// Update is called once per frame
void Update () {
int horizontal = 0;
int vertical = 0;
horizontal = (int)Input.GetAxisRaw("Horizontal");
vertical = (int)Input.GetAxisRaw("Vertical");
if(timer == 0)
{
if(horizontal != 0)
vertical = 0;
if(horizontal != 0 || vertical != 0)
Move(horizontal, vertical);
}
if(!Input.anyKey)
{
timer = 0;
}
else
{
timer += 1;
}
}
}
---A little code to add some random movement to the limbs.
public class RagdollForceScript : MonoBehaviour {
public float force = 0.0f;
public float lowForce = -0.05f;
public float highForce = 0.05f;
public int counter = 0;
public Component[] rbLimbs;
// Use this for initialization
void Awake () {
rbLimbs = GetComponentsInChildren<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
force = Random.Range(lowForce, highForce);
if(counter > 2)
{
foreach(Rigidbody rb in rbLimbs)
{
rb.AddForce(force,force,force);
counter = 0;
}
}
else
{
counter ++;
}
}
}
Your answer
Follow this Question
Related Questions
physics.OverlapSphere colliders 1 Answer
create character colliders automatically 0 Answers
How can I disable the collider collision of a ragdolls specific bones? 0 Answers
Ragdoll stucks in objects 0 Answers
Ragdoll with multiple colliders 0 Answers