Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question The character does not animate

Discussion in 'Animation' started by Vini310, Jun 2, 2020.

  1. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    I'm having a weird problem to animate a character in my project. For reference, the character, an enemy in the game, has three animations: "Idle" (not really an animation, but that's how the character stays when the player's not around), "ArmMoving" (for when the player is coming closer to the enemy) and "Karate" (when the player is even closer to the enemy).
    For the transition from "Idle" to "ArmMoving", a bool, called "NearPlayer" was created. If true, the "ArmMoving" animation starts, if false, it goes back to "Idle".
    And the, for the transtion from "ArmMoving" to "Karate", I created another bool, "DangerArea", same scheme as before: if true, "Karate" starts, if false, goes back to "ArmMoving".
    Looks fine, right? Well, for some reason, none of the animations play when I playtest the game, even if I turn all the bools on.

    EDIT: I added a new bool, called "inIdle", and now this boll needs to be "false" for "Idle" to transit to "ArmMoving", and "true" for the reverse, I also made an script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RollController : MonoBehaviour
    6. {
    7.     static Animator anim;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         anim = GetComponent<Animator>;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if(Vector3.Distance(player.position, this.transform.position) < 10)
    18.         {
    19.             Vector3 direction = player.position - this.transform.position;
    20.             direction.y = 0;
    21.             anim.SetBool("isIdle", false);
    22.             if(direction.magnitude >= 5)
    23.            
    24.                 anim.SetBool("NearPlayer", true);
    25.                 anim.SetBool("isIdle", false);
    26.             }
    27.             if(direction.magnitude > 3)
    28.             {
    29.                 anim.SetBool("DangerArea", true);
    30.             }
    31.             else
    32.             {
    33.                 anim.SetBool("isIdle", false);
    34.                 anim.SetBool("NearPlayer", false);
    35.                 anim.SetBool("DangerArea", false);
    36.             }
    37.         }
    38.     }
    39. }
    I'm now getting the error "Type or namespace definition, or end-of-life expected".
     
    Last edited: Jun 4, 2020
  2. spylefkaditis

    spylefkaditis

    Joined:
    Jul 3, 2016
    Posts:
    22
    Can you share some pics of the setup?
     
  3. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
  4. spylefkaditis

    spylefkaditis

    Joined:
    Jul 3, 2016
    Posts:
    22
    Is that the whole script?

    Line 11 should be
    anim = GetComponent<Animator>();

    Also the variable player isn't declared anywhere.
    The variable direction is not accessible outside of the scope of the if statement.

    Also you are saying if the direction.magnitude >= 5 then NearPlayer = true and isIdle = false
    After that you are saying if direction.magnitude > 3 then DangerArea = true otherwise you reset them all to false.

    I think you need to look at your if statements 'cause I'm suspicious that they're not doing what you think they are.

    Also the vector magnitude and distance are the same thing so you only need 1 not both.

    Not sure if the logic is what you want here, but try this

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Test : MonoBehaviour
    4. {
    5.     [SerializeField] Transform target;
    6.  
    7.     Animator anim;
    8.  
    9.     void Start()
    10.     {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         float distance = Vector3.Distance(target.position, transform.position);
    17.  
    18.         if (distance < 10)
    19.         {
    20.             if (distance >= 5)
    21.             {
    22.                 anim.SetBool("isIdle", false);
    23.                 anim.SetBool("DangerArea", false);
    24.                 anim.SetBool("NearPlayer", true);
    25.             }
    26.  
    27.             if(distance < 5)
    28.             {
    29.                 anim.SetBool("NearPlayer", false);
    30.                 anim.SetBool("DangerArea", true);
    31.             }
    32.         } else
    33.         {
    34.             anim.SetBool("isIdle", true);
    35.             anim.SetBool("NearPlayer", false);
    36.             anim.SetBool("DangerArea", false);
    37.         }
    38.     }
    39. }
    40.  
     
    Last edited: Jun 4, 2020
  5. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    I tested and seems to be working just fine, I say SEEMS because the player character's models isn't ready yet, so I'm testing with a placeholder. And speaking of player:

    Code (CSharp):
    1. public class NaomiMovement : MonoBehaviour
    2. {
    3.     public float moveSpeed;
    4.     public float jumpForce;
    5.    
    6.     public Rigidbody myRigidbody;
    7.    
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         myRigidbody = GetComponent<Rigidbody>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
    18.        
    19.         if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown (0))
    20.         {
    21.             myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
    22.         }
    23.     }
    24. }
    For context, I'm creating a runner game, meaning that the character will be moving constantly, theplayer is only able to control the jumps and attacks (the latter aren't programmed). Despite being made in 3D and using models, it still uses 2D movement (like in Super Mario Run for example), so much that the script I'm using was taken from this video:
    only converted to be used in 3D, and now I'm having a weird problem:

    The character is moving to the left, rather than right, and it's going through the ground, which shouldn't happen because the ground is programmed with Box Collider!