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

Adding to Animation script

Discussion in 'Scripting' started by kingdom216, Jun 18, 2021.

  1. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    What can I add to this to set up keycodes for jump, scoop, tackle etc?

    I have done the animation in Unity's animator for other animations such as jump. I tried to use the same exact set up by copying the code below and changing isWalkingHash to isJumpingHash and followed suit for the rest of the code making the necessary changes to coincide with isJumpingHash. I've set bools, and floats but nothing seems to work, does anyone have any ideas besides watching tutorials on YouTube? Here is the original tutorial I followed for this script:


    Code (Csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class animationStateController : MonoBehaviour
    7. {
    8.     Animator animator;
    9.     int isWalkingHash;
    10.     int isRunningHash;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         animator = GetComponent<Animator>();
    16.         isWalkingHash = Animator.StringToHash("isWalking");
    17.         isRunningHash = Animator.StringToHash("isRunning");
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         bool isrunning = animator.GetBool(isRunningHash);
    24.         bool isWalking = animator.GetBool(isWalkingHash);
    25.         bool forwardPressed = Input.GetKey("w");
    26.         bool runPressed = Input.GetKey("left shift");
    27.  
    28.         if (!isWalking && forwardPressed)
    29.         {
    30.             animator.SetBool(isWalkingHash, true);
    31.         }
    32.         if (isWalking && !forwardPressed)
    33.         {
    34.             animator.SetBool(isWalkingHash, false);
    35.         }
    36.  
    37.         if (!isrunning && (forwardPressed && runPressed))
    38.         {
    39.             animator.SetBool(isRunningHash, true);
    40.         }
    41.         if (isrunning && (!forwardPressed || !runPressed))
    42.         {
    43.             animator.SetBool(isRunningHash, false);
    44.         }
    45.     }
    46.  
    47. }
    48.  
     
    Last edited: Jun 24, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    The script side of things will be trivial: it will will be something like what you already have above, but it will be entirely dependent on how your animation is set up for each of those things.

    If you set up the new animations in the same way you set up the existing ones, it will likely be just copy/paste code.

    If you didn't set up the above animations, then get yourself to some Youtube animation in Unity tutorials and learn how it was done, then you can manipulate those new animations from this script.
     
  3. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    I don't like your replies, you don't answer anything. I have done all the things within the animator on Unity that need to be done. I have done the same script for the other movements, basically just changing the key code from w and left shift to space bar and right shift, and the words from walk and run to tackle and jump. If that had worked I would not be here. Please do not answer my posts unless you have a valid response pertaining to my discussion, you are not adding anything to the post.
     
  4. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    We don't understand what is stopping you from adding this functionality yourself.

    Your original post suggests you don't know how to add these features, but your response suggests that you actually do know how and have tried, but couldn't get it to work.

    If you don't know where to start, "do some tutorials" is the correct response. If you tried but ran into problems, then you should be asking for help with those problems.

    What is the actual issue you are having?
     
    Kurt-Dekker likes this.
  5. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    I've done the tutorials, I've tried adding the functionalities myself but they are not working, is this not a place where people actually know how to help, or is it just a place where people tell you to watch youtube?

    I tried adding lines of Int, bools and floats for jumping and other animations, but they don't work when I am in play mode. I am asking for comprehensive suggestions. Again, I have done the tutorials on this, it is where I found the code above, but it does not go further than walking and running. When I apply the above principles to other functions, they don't work. So I am asking if anyone knows what lines to write that will enable my characters to do other functions besides walking and running. Is this clear enough for you, do I need to elaborate further?
     
  6. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    I am commenting my difficulties in the script:

    Code (Csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class animationStateController : MonoBehaviour
    7. {
    8.     Animator animator;
    9.     int isWalkingHash;
    10.     int isRunningHash;
    11.     int isJumpingHash;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         animator = GetComponent<Animator>();
    17.         isWalkingHash = Animator.StringToHash("isWalking");
    18.         isRunningHash = Animator.StringToHash("isRunning");
    19.         isJumpingHash = animator.StringToHash("isJumping");
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         bool isrunning = animator.GetBool(isRunningHash);
    26.         bool isWalking = animator.GetBool(isWalkingHash);
    27.         bool isJumping = animator.GetBool(isJumpingHash);
    28.         bool forwardPressed = Input.GetKey("w");
    29.         bool runPressed = Input.GetKey("left shift");
    30.         bool jumpPressed = Input.GetKey("space bar");
    31.        
    32. //this is where I am needing help, how do I phrase this to add in Jumping?
    33.         if (!isWalking && forwardPressed)
    34.         {
    35.             animator.SetBool(isWalkingHash, true);
    36.         }
    37.         if (isWalking && !forwardPressed)
    38.         {
    39.             animator.SetBool(isWalkingHash, false);
    40.         }
    41.  
    42.         if (!isrunning && (forwardPressed && runPressed))
    43.         {
    44.             animator.SetBool(isRunningHash, true);
    45.         }
    46.         if (isrunning && (!forwardPressed || !runPressed))
    47.         {
    48.             animator.SetBool(isRunningHash, false);
    49.         }
    50.     }
    51.    
    52. }
    53.  
     
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Please, in the future, post your attempt and a description of the problems first, not several posts into a thread.

    From the code side of things, all you need to do is set isJumping to true of the W key is held down:

    Code (CSharp):
    1. animator.SetBool(isJumpingHash, jumpPressed);
    You then have to set up your animation controller to play the jump animation when that variable is true.
     
    Kurt-Dekker likes this.
  8. kingdom216

    kingdom216

    Joined:
    Apr 13, 2021
    Posts:
    45
    This is no longer an issue for me because I have set up animation controls through the Input System and assigning bindings has made all of the difference. Thanks to all who attempted to help, and sorry for being short in my responses, but this is a frustrating business and when it feels like the answers that people are giving aren't addressing the concerns of the poster one tends to get more frustrated. I didn't word the post correctly in the beginning, but I am a novice so it takes work to learn how to even ask for help in this business.