Search Unity

Question Object deforming when pressed with Rigged hand model

Discussion in 'Scripting' started by Aniket_Jangam, May 13, 2023.

  1. Aniket_Jangam

    Aniket_Jangam

    Joined:
    Jan 29, 2023
    Posts:
    1
    Hello all, I am working on a physics based Rigged hand model which detects collision when touches any object and then attaches to the object to be able to pick it up. This feature came as a part of the purchased package. Now, I added a Deforming script to the object to deform the vertices when a collision is detected. But, due to the AttachObject script, I guess the collision forces are ignored and the object does not get deformed. Can anyone guide me how to go about this issue? I am new to Unity and need to resolve this ASAP. Thank you so much.


    Code (CSharp):
    1.  void AttachObject(Rigidbody objectToAttach, FingerType holdingFinger1, FingerType holdingFinger2)
    2.    {
    3.        _lastForearmPosition = Parts.Forearm.transform.position;
    4.  
    5.        Parts.Fingers[(int) holdingFinger1].IsHoldingObject = Parts.Fingers[(int)holdingFinger2].IsHoldingObject = true;
    6.        AttachedObject = objectToAttach.gameObject.AddComponent<AttachedObject>();
    7.        AttachedObject.Attach(this, DetachObject);
    8.        Parts.IgnoreCollisions(objectToAttach, true);
    9.  
    10.  
    11.        OnObjectAttached(objectToAttach);
    12.    }
    and Deformable script-
    Code (CSharp):
    1. private void OnCollisionEnter(Collision collision)
    2.    {
    3.        //Get point, impulse mag, and normal
    4.        Vector3 pt = transform.InverseTransformPoint(collision.contacts[0].point);
    5.        Vector3 nrm = transform.InverseTransformDirection(collision.contacts[0].normal);
    6.        float imp = (collision.impulse.magnitude * 100);
    7.        Vector3 collisionForce = collision.impulse / Time.fixedDeltaTime;
    8.        Debug.Log (collisionForce);
    9.        if (imp < minImpulse)
    10.            Debug.Log ("Force is less.");
    11.            return;
    12.  
    13.        //Deform vertices
    14.        verts = m.vertices;
    15.        //Debug.Log (verts.Length);
    16.        float scale; ///Declare outside of tight loop
    17.        for (int i = 0; i < verts.Length; i++)
    18.        {
    19.            //Get deformation scale based on distance
    20.            scale = Mathf.Clamp(radius - (pt - verts[i]).magnitude, 0, radius);
    21.            Debug.Log (radius - (pt - verts[i]).magnitude);
    22.            //Deform by impulse multiplied by scale and strength parameter
    23.            verts[i] += nrm * imp * scale * malleability;
    24.        }
     
  2. Fatferret

    Fatferret

    Joined:
    Apr 20, 2013
    Posts:
    18
    m.vertices returns a copy of the data and not a reference so you need to assign verts to m.vertices at the end and call UploadMeshData() to apply the changes