Search Unity

Collided arrow with ragdoll causes offset

Discussion in 'Physics' started by Eggtooth, Feb 11, 2019.

  1. Eggtooth

    Eggtooth

    Joined:
    Dec 29, 2015
    Posts:
    3
    Hello!

    I am new to unity. I have been making my own personal project, or personal goal of remaking certain game on Unity to see if I am up for task to creating my own later down the road with my own assets. Of course it's only prototype and such but I had good results, however I am stuck on a thing. Not gonna release it to public since it uses 3D assets (I know it's wrong, however I am not gonna release or otherwise get anything out of it.)

    So I have written a bow (in this case Stake gun) that shoots arrows, to work just like in original title, however, upon collision with part of ragdoll, say collider of arm, head of torso (and ragdoll consists of colliders, configurable joints), it causes the stake (or arrow) to snap completely wrong to the model, for example from behind, or sometiems when hitting head, far up in the air.

    Upon hitting the object, arrow is being attached to the part of ragdoll, see below.

    http://prntscr.com/mjmlxa
    It is however attached from the wrong side (not from front), rotation and position are completely wrong here. Basically I want this "arrow" to stick to body and remain at the position where it hit initially so it looks just like in original title.

    Below is hierarchy when it's attached to bone "k_zebra", which is torso, but offset is completely off.
    http://prntscr.com/mjmoy5

    here is gif:
    https://imgur.com/a/SRaucYn

    It also happens without parenting (object has weird offset).

    Instead of attaching as it should, from the direction the arrow hit, it completely changes the position as if hit from completely different angle, or just flat out floats in the air while attached.

    I am attaching the arrow code. Could someone help? I don't know how to proceed.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityStandardAssets.Characters.FirstPerson;
    5. using UnityEngine.AI;
    6.  
    7. public class StakeScript : MonoBehaviour {
    8.  
    9.     public MeshRenderer kolekMesh;
    10.     public bool isVisible = false;
    11.     public StakeGunScript stakeGun;
    12.     public bool hitSomething;
    13.     public bool hitBody;
    14.     public Rigidbody rb;
    15.     AudioSource audSrc;
    16.     public ConfigurableJoint jointAttach;
    17.     public AudioClip[] hitSound;
    18.     private AudioClip hit;
    19.     Transform kolekObject;
    20.     [HideInInspector]
    21.     public EnemyHealth health;
    22.     [HideInInspector]
    23.     public RagdollHelper ragdollHelper;
    24.     [HideInInspector]
    25.     public CapsuleCollider healthCollider;
    26.     [HideInInspector]
    27.     public Rigidbody rigidBodyForForce;
    28.     [HideInInspector]
    29.     public NavMeshAgent nav;
    30.     [HideInInspector]
    31.     bool isHurt;
    32.     [HideInInspector]
    33.     public Rigidbody rootRigidBodyDisable;
    34.     public float hitForceHorizontal;
    35.     public float hitForceVertical;
    36.     bool snapped;
    37.  
    38.  
    39.  
    40.     // Use this for initialization
    41.     void Start () {
    42.         kolekMesh = this.gameObject.GetComponent<MeshRenderer> ();
    43.         stakeGun = GameObject.FindGameObjectWithTag ("StakeGun").GetComponent<StakeGunScript> ();
    44.         rb = this.gameObject.GetComponent<Rigidbody> ();
    45.         audSrc = this.gameObject.GetComponent<AudioSource> ();
    46.         kolekObject = this.gameObject.transform;
    47. //        jointAttach = this.gameObject.GetComponent<ConfigurableJoint> ();
    48.     }
    49.  
    50.     void Update ()
    51.     {
    52. //        if (stakeGun.isShooting == false) {
    53. //            isVisible = false;
    54. //        }
    55. //
    56. //        if (stakeGun.isShooting == true) {
    57. //            StartCoroutine (visibilityTimer ());
    58. //        }
    59.  
    60.         StartCoroutine(RemoveObject());
    61.         if (!hitSomething) {
    62.             Vector3 vel = GetComponent<Rigidbody> ().velocity;
    63.             transform.rotation = Quaternion.LookRotation (vel);
    64.         }
    65.  
    66.         if (hitBody) {
    67.             rb.isKinematic = true;
    68. //
    69. //            Vector3 vel = GetComponent<Rigidbody> ().velocity;
    70. //            transform.rotation = Quaternion.LookRotation (vel);
    71.         }
    72.  
    73.     }
    74.  
    75.     void OnCollisionEnter(Collision col){
    76.  
    77.         if (col.gameObject.tag != "EnemyRagdollCollider"){
    78.         hitSomething = true;
    79.             if (hitSomething && !hitBody) {
    80.                 kolekObject.GetComponent<CapsuleCollider> ().enabled = false;
    81.                 StickNoPhysics ();
    82.                 int index = Random.Range (0, hitSound.Length);
    83.                 hit = hitSound [index];
    84.                 audSrc.clip = hit;
    85.                 audSrc.Play ();
    86.             }
    87.         }
    88.  
    89.         //this is where arrow stick to body
    90.         if (col.rigidbody) {
    91.             hitBody = true;
    92.             if (hitBody) {
    93.                 snapped = true;
    94.                 Vector3 vel = GetComponent<Rigidbody> ().velocity;
    95.                 transform.rotation = Quaternion.LookRotation (vel);
    96.                 Debug.Log ("Hit");
    97.  
    98.                 if (snapped) {
    99.  
    100.                     //parents the arrow to body (while on update it turns on isKinematic)
    101.                     this.transform.parent = col.transform;
    102.  
    103.                   //unused, tried already
    104. //                    kolekObject.transform.localPosition = col.transform.localPosition;
    105. //                    Vector3 stakeDirection = kolekObject.transform.forward;
    106. //                    Quaternion stakeRotation = Quaternion.LookRotation (vel);
    107. //                    this.transform.localPosition = stakeDirection;
    108. //                    this.transform.localRotation = stakeRotation;
    109.  
    110.                     kolekObject.GetComponent<CapsuleCollider> ().enabled = false;
    111.                 }
    112.  
    113.                 health = col.transform.root.gameObject.GetComponent<EnemyHealth> ();
    114.                 ragdollHelper = col.transform.root.GetComponent<Collider> ().gameObject.GetComponent<RagdollHelper> ();
    115.                 nav = col.transform.root.GetComponent<Collider> ().gameObject.GetComponent<NavMeshAgent> ();
    116.                 rootRigidBodyDisable = col.transform.root.GetComponent<Collider> ().gameObject.GetComponent<Rigidbody> ();
    117.  
    118.                 health.health -= 100;
    119.                 if (health.health <= 0) {
    120.                     aiScript.enabled = false;
    121.                     nav.enabled = false;
    122.                     ragdollHelper.ragdolled = true;
    123.                     rootRigidBodyDisable.isKinematic = true;
    124.                 }
    125.                 if (health.health <= 0 && health.isDead == false) {
    126.                     if (col.collider.tag == "EnemyRagdollCollider") {
    127.                         Debug.Log ("Added Force to body");
    128. //                        col.rigidbody.AddForce (transform.up * hitForceVertical);
    129. //                        col.rigidbody.AddForceAtPosition (stakeDirection * hitForceHorizontal, stakeRotation * stakeDirection * hitForceHorizontal);
    130.                     }
    131.                 }
    132.  
    133.             }
    134.         }
    135. }
    136.    
    137.  
    138.     public void StickNoPhysics(){
    139.         if (hitSomething) {
    140.             rb.constraints = RigidbodyConstraints.FreezeAll;
    141.         }
    142.  
    143.     }
    144.  
    145.     public void StickPhysics(){
    146.         rb.constraints = RigidbodyConstraints.FreezeAll;
    147.     }
    148.    
    149.     IEnumerator RemoveObject(){
    150.         yield return new WaitForSeconds (10f);
    151.         Destroy (this.gameObject);
    152.     }
    153. }
     
    Last edited: Feb 11, 2019
  2. W1zzel

    W1zzel

    Joined:
    Aug 7, 2017
    Posts:
    5
    Hi, have you found a solution?