Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Animate character using joint locations and C#?

Discussion in 'Animation' started by unity_AVTbeVl_8J6yfA, Aug 10, 2018.

  1. unity_AVTbeVl_8J6yfA

    unity_AVTbeVl_8J6yfA

    Joined:
    Aug 10, 2018
    Posts:
    2
    Hey everyone,

    I'm a complete beginner on Unity, and could use some pointers for a task I'm trying to accomplish.

    I have an array containing joint locations for 30 frames of data, and want to use it to animate a character from a script (I downloaded a couple random humanoid characters from the Unity asset store). However, I have no prior experience from Unity and have no idea how to do this. I have prior programming experience in Python and C++, so I should be able to get a decent grasp on C#.

    If anyone could give me some guidance, that would be greatly appreciated. Thanks.
     
  2. unity_AVTbeVl_8J6yfA

    unity_AVTbeVl_8J6yfA

    Joined:
    Aug 10, 2018
    Posts:
    2
  3. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    634
    I don't know how it would turn out, but basically you'll need to set the joints' rotation (I think you only need the position of the hip bone)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class frameseqBones : MonoBehaviour {
    6.  
    7.     [System.Serializable]
    8.     public class Bones
    9.     {
    10.         public Transform Hips;
    11.         public Transform Spine;
    12.         public Transform Chest;
    13.         public Transform UpperChest;
    14.         public Transform Neck;
    15.         public Transform Head;
    16.         public Transform LeftShoulder;
    17.         public Transform LeftUpperArm;
    18.         public Transform LeftLowerArm;
    19.         public Transform LeftHand;
    20.         public Transform RightShoulder;
    21.         public Transform RightUpperArm;
    22.         public Transform RightLowerArm;
    23.         public Transform RightHand;
    24.         public Transform LeftUpperLeg;
    25.         public Transform LeftLowerLeg;
    26.         public Transform LeftFoot;
    27.         public Transform RightUpperLeg;
    28.         public Transform RightLowerLeg;
    29.         public Transform RightFoot;
    30.  
    31.     }
    32.     public Bones bones = new Bones();
    33.  
    34.     //here you assign the angles
    35.     [System.Serializable]
    36.     public class Frames
    37.     {
    38.         public Vector3[] Hips;
    39.         public Vector3[] Spine;
    40.         public Vector3[] Chest;
    41.         public Vector3[] UpperChest;
    42.         public Vector3[] Neck;
    43.         public Vector3[] Head;
    44.         public Vector3[] LeftShoulder;
    45.         public Vector3[] LeftUpperArm;
    46.         public Vector3[] LeftLowerArm;
    47.         public Vector3[] LeftHand;
    48.         public Vector3[] RightShoulder;
    49.         public Vector3[] RightUpperArm;
    50.         public Vector3[] RightLowerArm;
    51.         public Vector3[] RightHand;
    52.         public Vector3[] LeftUpperLeg;
    53.         public Vector3[] LeftLowerLeg;
    54.         public Vector3[] LeftFoot;
    55.         public Vector3[] RightUpperLeg;
    56.         public Vector3[] RightLowerLeg;
    57.         public Vector3[] RightFoot;
    58.     }
    59.     public Frames frames = new Frames();
    60.  
    61.     public int x;//frame counter
    62.     public float deltax;
    63.     public float timer = 0.5f;
    64.  
    65.     //and in Update set their rotation
    66.  
    67.     void Update(){
    68.  
    69.         deltax += Time.deltaTime;
    70.         if(deltax>timer) {
    71.             deltax=0;
    72.             x+=1;
    73.         }
    74.  
    75.         bones.Hips.eulerAngles = new Vector3(frames.Hips[x].x, frames.Hips[x].y, frames.Hips[x].z);
    76.         bones.Spine.eulerAngles = new Vector3(frames.Spine[x].x, frames.Spine[x].y, frames.Spine[x].z);
    77.         //etc.
    78.        
    79.     }
    80. }
    81.