Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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 at 3:47 AM.

  1. unity_AVTbeVl_8J6yfA

    unity_AVTbeVl_8J6yfA

    Joined:
    Friday
    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:
    Friday
    Posts:
    2
  3. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    611
    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.