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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Trying to spawn a "chain" but my characterJoint doesnt seem to work properly. Please help!

Discussion in 'Scripting' started by Bicaro, Aug 3, 2018.

  1. Bicaro

    Bicaro

    Joined:
    Aug 12, 2017
    Posts:
    8
    So when I click on an object with a tag "Moveable" it's supposed to create a chain out of little 0.3x0.3x0.3 balls, and the number of balls is distance/4, and the ditsance between the balls is the offSet. So my balls spawn but every 3rd or every one is connected to the Player's inner ball (so the chain can go around it and not mess with physics) but they're supposed to be connected to the previous one in the chain. Please help!
    Also if you know how I could make the chain spawn rotated towards the object, thank you!

    Here is the code:

    Code (CSharp):
    1.  void Update ()
    2.     {
    3.         if(Input.GetMouseButton(0))
    4.         {
    5.             Rigidbody previousRB = hook.GetComponent<Rigidbody>();
    6.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);          
    7.  
    8.             if (Physics.Raycast(ray, out hit))
    9.             {            
    10.                 if (hit.transform.tag == "Moveable")
    11.                 {
    12.                     distance = Vector3.Distance(transform.position, hit.transform.position);
    13.                     distance = Mathf.Round(distance / 4);
    14.  
    15.                     for (int i = 0; i < distance; i++)
    16.                     {
    17.                         GameObject link = Instantiate(linkPrefab, transform.position + offSet, Quaternion.Euler(new Vector3(0,0,0)));
    18.                         CharacterJoint joint = link.GetComponent<CharacterJoint>();
    19.                         joint.connectedBody = previousRB;
    20.                         joint.autoConfigureConnectedAnchor = false;
    21.  
    22.                         offSet += offSetAdd;
    23.                         previousRB = link.GetComponent<Rigidbody>();
    24.                     }
    25.                 }
    26.             }
    27.         }
    28.     }
     
    Last edited: Aug 3, 2018
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    I made a "FollowEnTrail" package that might be helpful here.

    It intentionally does not use physics.
     

    Attached Files:

  3. Bicaro

    Bicaro

    Joined:
    Aug 12, 2017
    Posts:
    8
    Ok thanks!