Search Unity

Making controlled character

Discussion in 'Animation' started by placide385, Aug 30, 2018.

  1. placide385

    placide385

    Joined:
    Aug 29, 2018
    Posts:
    3
    I have been trying to apply some animations I made and turn it into a game character. It has been working okay except for some problems.
    First, the character comes in unity missing it's eyes. I ignored that then moved onto the movement of the character. When a translation happens the correct walk animation starts but the characters placement in the world just keeps snapping back to it's original position.



    If it helps i'll also leave the code for the movements

    Code (CSharp):
    1. using System.Collections;
    2. using Systems.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.    
    7.     Rigidbody rb;
    8.     float speed = 50.0F;
    9.     float rotationSpeed = 80.0F;
    10.     Animator animator;
    11.  
    12.     void Start(){
    13.         rb = this.GetComponent<Rigidbody>();
    14.         animator = GetComponentInChildren<Animator>();
    15.         animator.SetBool("Idling", true);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void LateUpdate () {
    20.  
    21.         float translation = Input.GetAxis("Vertical") * speed;
    22.         float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    23.         translation *= Time.deltaTime;
    24.         rotation *= Time.deltaTime;
    25.         Quaternion turn = Quaternion.Euler(0f,rotation,0f);
    26.         rb.MoveRotation(rb.rotation * turn);
    27.         animator.SetFloat("SpeedMult",translation);
    28.  
    29.         if(translation != 0)
    30.             animator.SetBool("Idling", false);
    31.         else
    32.             animator.SetBool("Idling", true);
    33.            
    34.         if (Input.GetKeyDown ("space"))
    35.         {
    36.             animator.SetTrigger ("Sit");  
    37.         }
    38.  
    39.         if (Input.GetKeyDown ("f"))
    40.         {
    41.             animator.SetTrigger ("Flip");  
    42.         }  
    43.     }
    44. }
     
  2. IvanDonets

    IvanDonets

    Joined:
    Feb 19, 2014
    Posts:
    117
    is there movement of whole dog forward in walk animation itself? if yes, it can be a rpoblem. not a code. As far as I know, the movement forward should be made completely in code, and it should not be there in walk animation file.
     
  3. placide385

    placide385

    Joined:
    Aug 29, 2018
    Posts:
    3
    This one does have movement. I used the one with no root movement first but that animation freezes after one cycle and the character won't move or go back to it's idling position.
     
  4. IvanDonets

    IvanDonets

    Joined:
    Feb 19, 2014
    Posts:
    117
    To make character animation switch from playing animation to other - is another thing. I tried using "state machines" for that, making conditions, but I didn't find that flexible enough.

    as for freezing animation after one cycle. Well, if you make moving forward, walking, then you should make walking function. In Update() make "if" block to check for pressing "cursor up" etc, and call the walk function.

    And in walk function, you should play animation. But I'ld better don't use SetBool for that. I don't find it very flexible. Instead I directly set name of Animation to "walk". And also you have to add movement along the axis "backward-forward". Something like "x = x + 1". So if you hold cursor up, dog will move forward. Even if there will be no animation, it should move forward.
     
    placide385 likes this.
  5. placide385

    placide385

    Joined:
    Aug 29, 2018
    Posts:
    3
    So it looks like I am going to need to make some adjustments the script. Thanks for the advice!