Search Unity

Blending FBX animations and coding them properly in the first place

Discussion in 'Animation' started by Progma_, Oct 14, 2018.

  1. Progma_

    Progma_

    Joined:
    Mar 1, 2018
    Posts:
    7
    I'm having trouble figuring out how to program and blend the fbx animations properly in my game. I made the animations through blender and imported them into unity through an fbx format. When I found out in a previous version of unity that I wasn't able to use the animator window to it's fullest extent with the fbx animations properly, I tried coding them in the playercontroller script. This works mostly, but due to my limited knowledge of scripting in C# I've ran into a few problems.

    Here is my script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PCCBackup: MonoBehaviour {
    5.     static Animator anim;
    6.     public bool walking;
    7.  
    8.     public GameObject playerModel, Hero;
    9.     //Transforms
    10.     public Transform playerCam, character, centerPoint;
    11.  
    12.     private Vector3 moveDirection;
    13.  
    14.     //character controller declaration
    15.     CharacterController player;
    16.     //Mouse Rotation
    17.     private float rotX, rotY;
    18.     //Mouse Y Position
    19.     public float mouseYPosition = 1f;
    20.     //Mouse Sensitivity
    21.     public float Sensitivity = 10f;
    22.     //Mouse Zoom
    23.     private float zoom;
    24.     public float zoomSpeed = 2;
    25.     //Clamping Zoom
    26.     public float zoomMin = -2f;
    27.     public float zoomMax = -10f;
    28.     public float rotationSpeed = 5f;
    29.     //Move Front Back left & Right
    30.     private float moveFB, moveLR;  
    31.     //Movement Speed
    32.     public float Speed = 2f;
    33.     //Velocity of Gravity
    34.     public float verticalVelocity;
    35.     //Jump Distance
    36.     public float jumpDist = 5f;
    37.     //Multiple Jumps
    38.     int jumpTimes;
    39.     //To use with Dialogue Manager
    40.     public DialogueManager DiagM;
    41.  
    42.     public AudioClip jumpSound;
    43.     public AudioClip HurtSound;
    44.  
    45.     AudioSource audioSource;
    46.  
    47.     //knockback
    48.  
    49.     public float knockBackForce;
    50.     public float knockBackTime;
    51.     private float knockBackCounter;
    52.  
    53.  
    54.     // Use this for initialization
    55.     void Start ()  
    56.     {
    57.         //character controller
    58.         player = GameObject.Find("Player").GetComponent<CharacterController> ();
    59.         anim = GetComponent<Animator>();
    60.      
    61.         //mouse zoom
    62.         zoom = -3;  
    63.         centerPoint.transform.position = playerCam.transform.position;
    64.         centerPoint.transform.parent = null;
    65.         audioSource = GetComponent<AudioSource>();
    66.     }
    67.     // Update is called once per frame
    68.     void Update ()
    69.     {      
    70.         //Mouse Zoom Input
    71.         zoom += Input.GetAxis ("Mouse ScrollWheel") * zoomSpeed;
    72.         if (zoom > zoomMin)
    73.             zoom = zoomMin;
    74.         if (zoom < zoomMax)
    75.             zoom = zoomMax;
    76.         //Mouse Camera Input
    77.         playerCam.transform.localPosition = new Vector3 (0, 0, zoom);
    78.         //Mouse Rotation
    79.                              
    80.         rotX += Input.GetAxis ("Mouse X") * Sensitivity;
    81.         rotY -= Input.GetAxis ("Mouse Y") * Sensitivity;    
    82.         //Clamp Camera
    83.         rotY = Mathf.Clamp (rotY, -60f, 60f);
    84.         playerCam.LookAt (centerPoint);
    85.         centerPoint.localRotation = Quaternion.Euler (rotY, rotX, 0);
    86.  
    87.         //Movement Speed
    88.         if (knockBackCounter <= 0)
    89.      {  
    90.         moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    91.         moveDirection = moveDirection * Speed;
    92.         moveDirection.y = verticalVelocity;
    93.  
    94.         player.Move(moveDirection * Time.deltaTime);
    95.        
    96.        
    97.         //Movement Rotation
    98.  
    99.         centerPoint.position = new Vector3 (character.position.x, character.position.y + mouseYPosition, character.position.z);
    100.  
    101.             //knockback disable
    102.             //Movement Input
    103.  
    104.             if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
    105.  
    106.             {
    107.                 transform.rotation = Quaternion.Euler(0f, centerPoint.rotation.eulerAngles.y, 0f);
    108.                 Quaternion turnAngle = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
    109.                 playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, turnAngle, Time.deltaTime * rotationSpeed);
    110.  
    111.                 if (player.isGrounded == true)
    112.  
    113.                 {
    114.                     anim.Play("Running");
    115.                 }
    116.             }
    117.             else
    118.             {
    119.                 if (player.isGrounded == true)
    120.                 {
    121.                     anim.Play("Idle");
    122.                 }
    123.                
    124.  
    125.             }
    126.  
    127.             if (Input.GetButtonDown("LHand"))
    128.             {
    129.                 anim.Play("Hello");
    130.             }
    131.  
    132.             if (Input.GetButtonDown("RHand"))
    133.             {
    134.                 anim.Play("RPunch");
    135.             }
    136.  
    137.             if (player.isGrounded == true)
    138.  
    139.             {
    140.                 jumpTimes = 0;
    141.                 verticalVelocity = -Physics.gravity.y * Time.deltaTime;
    142.             }
    143.  
    144.             else
    145.  
    146.             {
    147.                 verticalVelocity += Physics.gravity.y * Time.deltaTime;
    148.             }
    149.  
    150.          
    151.  
    152.             if (jumpTimes < 1)
    153.  
    154.             {
    155.                 if (Input.GetButtonDown("Jump"))
    156.                 {
    157.                     verticalVelocity += jumpDist;
    158.                     anim.Play("Jump");
    159.                     audioSource.PlayOneShot(jumpSound, 1F);
    160.                     jumpTimes += 1;
    161.                 }
    162.             }
    163.         }
    164.         else
    165.         {
    166.             knockBackCounter -= Time.deltaTime;
    167.         }
    168.  
    169.        
    170.  
    171.     }
    172.     public void Knockback(Vector3 direction)
    173.     {
    174.        
    175.         knockBackCounter = knockBackTime;
    176.         anim.Play("Jump");
    177.         audioSource.PlayOneShot(HurtSound, 50F);
    178.         moveDirection = direction * knockBackForce;
    179.         moveDirection.y = knockBackForce;
    180.     }
    181.  
    182. }
    1. Currently I'm trying to make the Hello and RPunch animations work while the character is grounded and running or idle. However, as the code is currently written the animations won't play properly due to the idle animation overriding them since the script is written to play the idle animation when the character is still and grounded. So I'm not sure how to deal with that. I'm currently researching coroutines to see if one will help.

    2. Once I figure that out, I don't know what I should learn afterwards in order to blend the running and jumping animations with the RPunch or Hello animation together either, as well as anything else I come up with that should blend together with running or jumping. I've had a friend say to try mixamo but since my character is not exactly human (a bipedal mushroom with two arms and legs whose face is on its torso, similar to spongebob) it did not seem to like that. I didn't want to try it further yet due to me being cautious.

    Any help on this matter would be greatly appreciated. I've only started to learn this stuff since last February. I'll be sure to provide anymore details if needed.