Search Unity

animation scripting problem driveing me nuts

Discussion in 'Scripting' started by yaramaris95, Sep 13, 2019.

  1. yaramaris95

    yaramaris95

    Joined:
    May 27, 2019
    Posts:
    18
    Hello everyone,

    I am in the proces of puting the animations in place so I can start on things like farming and stuff. Now i ran into a problem as to wich i can't find a solution. Please keep in mind im fairly new to this all and ithe fix could be obviouse to you but not to me.

    so now for the problem. with this code the idle animation is played when it needs to idle and when the walk anim should play. and it runs when it should run(with pressing LeftShift + W) line 64 and 65 take care of that.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed = 2f;
    8.     public float runSpeed = 6f;
    9.     public float jumpSpeed = 6f;
    10.     public float gravity = 20f;
    11.  
    12.     private Vector3 moveDirection = Vector3.zero;
    13.     private int jumps;
    14.     private CharacterController controller;
    15.     private Animator anim;
    16.  
    17.     void Start()
    18.     {
    19.        
    20.         controller = GetComponent<CharacterController> ();
    21.         anim = GetComponent<Animator> ();
    22.         controller.enabled = true;
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if (controller.isGrounded) {
    28.             print ("Grounded");
    29.         } else {
    30.             print ("Not Grounded");
    31.         }
    32.  
    33.         if (controller.isGrounded) {
    34.             moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
    35.             moveDirection = transform.TransformDirection (moveDirection);
    36.             moveDirection *= speed;
    37.             anim.SetBool ("isWalking", true);
    38.  
    39.  
    40.             if (Input.GetButtonDown ("Jump")) {
    41.                 moveDirection.y = jumpSpeed;
    42.             }
    43.             jumps = 0;
    44.         }
    45.  
    46.         if ((Input.GetKey (KeyCode.LeftShift)) && controller.isGrounded){
    47.             moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    48.             moveDirection = transform.TransformDirection (moveDirection);
    49.             moveDirection *= runSpeed;
    50.             anim.SetBool ("isRunning", true);
    51.  
    52.             if (Input.GetButtonDown ("Jump")) {
    53.                 moveDirection.y = jumpSpeed;
    54.             }
    55.             jumps = 0;
    56.         }
    57.         else {
    58.             moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), moveDirection.y, Input.GetAxis ("Vertical"));
    59.             moveDirection = transform.TransformDirection (moveDirection);
    60.             moveDirection.x *= speed;
    61.             moveDirection.z *= speed;
    62.             moveDirection.x *= runSpeed;
    63.             moveDirection.z *= runSpeed;
    64.             anim.SetBool ("isWalking", false);
    65.             anim.SetBool ("isRunning", false);
    66.  
    67.             if (Input.GetButtonDown ("Jump") && jumps < 1) {
    68.                 moveDirection.y = jumpSpeed;
    69.                 jumps++;
    70.             }
    71.        
    72.         }
    73.  
    74.         moveDirection.y -= gravity * Time.deltaTime;
    75.         controller.Move (moveDirection * Time.deltaTime);
    76.     }
    77. }
    78.  
    79.  
    if i remove those lines (64 and 65) the walk animation will play all the time, so when it needs to idle it walks and when it reeds to run it walks.

    i realy hope someone can help me with this because i realy realy dont know what to do anymore.

    thanks in advance
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,528
    You probably just need to check against moveDirection.magnitude:
    Code (csharp):
    1.  
    2. moveDirection.z *= runSpeed;
    3.             anim.SetBool ("isWalking", moveDirection.magnitude > 1f);
    4.             anim.SetBool ("isRunning", moveDirection.magnitude > 2f);
    5.  
    6.             if (Input.GetButtonDown ("Jump") && jumps < 1) {
     
  3. yaramaris95

    yaramaris95

    Joined:
    May 27, 2019
    Posts:
    18
    unfortunatly this messes everything up more. now it walks and runs depending if i pres w,a,s or d
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,528
    Isn't that...uh... what's supposed to happen?
     
  5. yaramaris95

    yaramaris95

    Joined:
    May 27, 2019
    Posts:
    18
    I need the walk animation to play when I press w. This only happens when I remove line 64 an 65,but then my idle and run animations stop working and the character always walks even when standing still