Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Rig builder not working properly when using it from code.

Discussion in 'Animation Rigging' started by Borellun, Apr 21, 2021.

  1. Borellun

    Borellun

    Joined:
    Sep 1, 2018
    Posts:
    4
    Hi. So I am trying to do something like a game of snake. And I want to try using the Animation Rigging package, with RigBuilder and DampedTransform. My snake is currently composed of multiple spheres, and if I create them in the editor, they work as expected.

    My use case is that when I pick a fruit from the ground, I want to instantiate a new sphere (body part) and append it to the snake.

    I was able to instantiate the objects, and then did the Build() on the rig builder; by default, the distance between each body part is 1, but when I'm moving, the distance and rotation aren't constant, so if I pick a new fruit while I am turning, the new body part won't be instantiated in the right place (will have a weird offset).

    Is there a good fix for that, or should I drop using the Animation Rigging part on things like this?
     
  2. Borellun

    Borellun

    Joined:
    Sep 1, 2018
    Posts:
    4
    In case it helps, this is how the code I got looks like:

    Code (CSharp):
    1.  
    2. public class SnakeManager : MonoBehaviour {
    3.   private RigBuilder rigBuilder;
    4.  
    5.   [Header("Size Flow Components")]
    6.   [SerializeField] private Transform bodyContainer;
    7.   [SerializeField] private Transform rigContainer;
    8.   [SerializeField] private Transform head;
    9.  
    10.   [SerializeField] private GameObject bodyPartPrefab;
    11.   [SerializeField] private GameObject bodyRigPrefab;
    12.   [SerializeField] private int initialBodySize = 3;
    13.  
    14.   private void Awake() {
    15.     rigBuilder = GetComponent<RigBuilder>();
    16.    
    17.     for (int i = 0; i < initialBodySize; i++) { CreateBodyPart(); }
    18.   }
    19.  
    20.   public void CreateBodyPart() {
    21.     Transform lastBodyPart;
    22.     if (bodyContainer.childCount == 0) {
    23.       lastBodyPart = head;
    24.     } else {
    25.       lastBodyPart = bodyContainer.GetChild(bodyContainer.childCount - 1);
    26.     }
    27.  
    28.     GameObject newBodyPart = Instantiate(bodyPartPrefab, bodyContainer);
    29.     newBodyPart.transform.localPosition = new Vector3(0, 1, lastBodyPart.localPosition.z - 1);
    30.     newBodyPart.transform.localRotation = lastBodyPart.localRotation;
    31.     newBodyPart.name = $"Body{bodyContainer.childCount}";
    32.  
    33.     GameObject newBodyRig = Instantiate(bodyRigPrefab, rigContainer);
    34.     newBodyRig.transform.localPosition = newBodyPart.transform.localPosition;
    35.     newBodyRig.transform.localRotation = newBodyPart.transform.localRotation;
    36.     newBodyRig.name = $"Body{rigContainer.childCount} Rig";
    37.  
    38.     DampedTransform dampedTransform = newBodyRig.GetComponent<DampedTransform>();
    39.     dampedTransform.data.constrainedObject = newBodyPart.transform;
    40.     dampedTransform.data.sourceObject = lastBodyPart.transform;
    41.     dampedTransform.data.dampPosition = 0.05f;
    42.     dampedTransform.data.dampRotation = 0.35f;
    43.     dampedTransform.data.maintainAim = true;
    44.  
    45.     rigBuilder.Build();
    46.   }
    47. }
    48.