Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I stop a Ragdoll passing through fast moving colliders?

Discussion in 'Physics' started by zeroiq50, Feb 24, 2015.

  1. zeroiq50

    zeroiq50

    Joined:
    Jun 15, 2013
    Posts:
    15
    G'day all,
    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...
    As I write this I am realizing that I haven't yet tried to disable the Y-axis lock on "impact" but I will still post this just in case someone has a better idea.

    I will try to attach a short video(Added as .zip can't do mp4) (I am manually controlling the approaching cube...) of what I am talking about 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.

    ---My movement code. soon to include "Swipe".
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerSwipeMovement : MonoBehaviour {
    5.  
    6.     public float moveTime = 0.1f;
    7.  
    8.     //private Rigidbody rBody;
    9.     private float inverseMoveTime;
    10.     private Vector2 touchOrigin = -Vector2.one;
    11.     public int timer = 0;
    12.  
    13.     public Component[] rgdBodys;
    14.  
    15.     private float angDrag;
    16.     private float oldDrag;
    17.  
    18.     void Start()
    19.     {
    20.         inverseMoveTime = 1f / moveTime;
    21.         rgdBodys = GetComponentsInChildren<Rigidbody>();
    22.     }
    23.  
    24.     void Move(int xDir, int zDir)
    25.     {
    26.         if(rgdBodys.Length == 0)
    27.         {
    28.             rgdBodys = GetComponentsInChildren<Rigidbody>();
    29.         }
    30.  
    31.         if(xDir > 0)
    32.             xDir = 1;
    33.         if(xDir < 0)
    34.             xDir = - 1;
    35.         if(zDir > 0)
    36.             zDir =  1;
    37.         if(zDir < 0)
    38.             zDir =  - 1;
    39.  
    40.         float newX = transform.position.x + xDir;
    41.         float newZ = transform.position.z + zDir;
    42.  
    43.         Vector3 start = new Vector3(transform.position.x, 0, transform.position.z);
    44.         Vector3 end = new Vector3(newX, 0, newZ);
    45.  
    46.         foreach(Rigidbody rBody in rgdBodys)
    47.         {
    48.             angDrag = rBody.GetComponent<Rigidbody>().angularDrag;
    49.             oldDrag = rBody.GetComponent<Rigidbody>().drag;
    50.             rBody.angularDrag = 1.0f;
    51.             rBody.drag = 3.0f;
    52.             rBody.MovePosition(end);
    53.  
    54.             if(rBody.position == end)
    55.             {
    56.                 rBody.drag = oldDrag;
    57.                 rBody.angularDrag = angDrag;
    58.             }
    59.         }
    60.         return;
    61.  
    62.     }
    63.  
    64.  
    65.     // Update is called once per frame
    66.     void Update () {
    67.    
    68.         int horizontal = 0;
    69.         int vertical = 0;
    70.  
    71.         horizontal = (int)Input.GetAxisRaw("Horizontal");
    72.         vertical = (int)Input.GetAxisRaw("Vertical");
    73.  
    74.  
    75.         if(timer == 0)
    76.         {
    77.             if(horizontal != 0)
    78.                 vertical = 0;
    79.  
    80.             if(horizontal != 0 || vertical != 0)
    81.                 Move(horizontal, vertical);
    82.         }
    83.         if(!Input.anyKey)
    84.         {
    85.             timer = 0;
    86.         }
    87.         else
    88.         {
    89.             timer += 1;
    90.         }
    91.     }
    92.  
    93.  
    94. }
    95.  

    ---A little code to add some random movement to the limbs.
    Code (CSharp):
    1. public class RagdollForceScript : MonoBehaviour {
    2.  
    3.     public float force = 0.0f;
    4.     public float lowForce = -0.05f;
    5.     public float highForce = 0.05f;
    6.     public int counter = 0;
    7.  
    8.     public Component[] rbLimbs;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Awake () {
    13.    
    14.         rbLimbs = GetComponentsInChildren<Rigidbody>();
    15.  
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void FixedUpdate () {
    21.    
    22.         force = Random.Range(lowForce, highForce);
    23.  
    24.         if(counter > 2)
    25.         {
    26.             foreach(Rigidbody rb in rbLimbs)
    27.             {
    28.                 rb.AddForce(force,force,force);
    29.                 counter = 0;
    30.             }
    31.         }
    32.         else
    33.         {
    34.             counter ++;
    35.         }
    36.     }
    37. }
     

    Attached Files:

  2. LSPressWorks

    LSPressWorks

    Joined:
    Jun 16, 2014
    Posts:
    25
    raycast last position to new position, if it hits anything, set coords to last position and set velocity.normalized to 0