Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Held Button animation problem

Discussion in '2D' started by Oddinx, Sep 25, 2017.

  1. Oddinx

    Oddinx

    Joined:
    Mar 2, 2017
    Posts:
    12
    Hi, I have a problem when I held the attack button of my character. I have a shoot animation, but I only want it to be displayed once the shot is fired and not while I'm holding the button down.

    This is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Move : MonoBehaviour {
    6.  
    7.     public float maxSpeed = 30;
    8.     public float speed = 5.0f;
    9.     public bool isOnground;
    10.  
    11.     Transform _transform;
    12.     Rigidbody2D rb;
    13.  
    14.      Animator anim;
    15.  
    16.  
    17.     public float jumpVelocity;
    18.  
    19.     public float fallm = 3.5f;
    20.     public float lowJumpm = 3f;
    21.  
    22.     //BALA
    23.  
    24.     public Transform firePoint;
    25.     public GameObject Bala;
    26.     public GameObject Bala2;
    27.     public float chargetimer = 0;
    28.  
    29.     public KeyCode chargeshoot;
    30.     public GameObject Charge;
    31.  
    32.     // Use this for initialization
    33.     void Start () {
    34.  
    35.         rb = GetComponent <Rigidbody2D> ();
    36.         anim = GetComponent <Animator> ();
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update () {
    41.  
    42.         //Move
    43.         anim.SetFloat("velX", Mathf.Abs(rb.velocity.x));
    44.         anim.SetBool ("Grounded", isOnground);
    45.    
    46.         //Salto
    47.         if (Input.GetButtonDown ("Jump") && isOnground) {
    48.  
    49.             rb.velocity = Vector2.up * jumpVelocity;
    50.             anim.SetBool ("shooting",false);
    51.         }
    52.         if (rb.velocity.y < 0) {
    53.  
    54.  
    55.             rb.velocity += Vector2.up * Physics2D.gravity.y * (fallm - 1) * Time.deltaTime;
    56.  
    57.  
    58.         } else if (rb.velocity.y > 0 && !Input.GetButton ("Jump")) {
    59.  
    60.             rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpm - 1) * Time.deltaTime;
    61.         }
    62.  
    63.     }
    64.  
    65.     void FixedUpdate (){
    66.    
    67.         //Move
    68.         float h = Input.GetAxis("Horizontal");
    69.  
    70.         if (h > 0.1f) {
    71.    
    72.             transform.localScale = new Vector3 (1f, 1f, 1f);
    73.         }
    74.         if (h < -0.1f) {
    75.  
    76.             transform.localScale = new Vector3 (-1f, 1f, 1f);
    77.         }
    78.  
    79.         rb.AddForce (Vector2.right * speed * h);
    80.  
    81.         float limitedSpeed = Mathf.Clamp (rb.velocity.x, -maxSpeed, maxSpeed);
    82.         rb.velocity = new Vector2 (limitedSpeed, rb.velocity.y);
    83.  
    84.  
    85.  
    86.         //Shoot
    87.         if (Input.GetKeyDown (KeyCode.A)) {
    88.        
    89.             Instantiate (Bala, firePoint.position, firePoint.rotation);
    90.             if (Input.GetKeyDown (KeyCode.A) && isOnground) {
    91.                 anim.SetBool ("shooting", true);
    92.             }
    93.  
    94.    
    95.         }
    96.  
    97.    
    98.         if (Input.GetKey (chargeshoot)) {
    99.             chargetimer += Time.deltaTime;
    100.        
    101.             }
    102.  
    103.         if(Input.GetKeyUp (chargeshoot) && (chargetimer > 1) )  {
    104.  
    105.  
    106.             Instantiate (Bala2, firePoint.position, firePoint.rotation);
    107.             chargetimer = 0;
    108.        
    109.             anim.SetBool ("shooting",false);
    110.         }
    111.  
    112.         if(Input.GetKeyUp (chargeshoot) && (chargetimer < 1)) {
    113.  
    114.             anim.SetBool ("shooting",false);
    115.             chargetimer = 0;
    116.        
    117.         }
    118.  
    119.  
    120.        
    121.         }
     
  2. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    (This isn't the problem you are actually facing, but i want to tell you there is a problem in your script
    Checking for Input in fixedUpdate might lead to the input being ignored because inputs get calculated during the Update. Fixed update gets called 50 times a second (usually), Update sometimes gets called faster. If you Input "A", there is some times an entire frame end before fixedUpdate gets called.)
    This is an old code i am using (it isn't perfect (the naming part and the crossinput part are totally wrong) and it's only a part of it but it has some ideas about how to make input to work better when using it in fixed update.
    Code (CSharp):
    1. public partial class PlatformerCharacterVX
    2.  
    3. {
    4.     [Header("@~~~~Input Settings (PlatformerCharacterInput.cs)")]
    5.  
    6.     [SerializeField] private List<string> buttons;
    7.  
    8.  
    9.  
    10.     [Header("_Inspector debugging")]
    11.     public List<Inputbutton> buttonlist;
    12.  
    13.     partial void StartInput()
    14.     {
    15.         buttonlist = new List<Inputbutton>();
    16.         foreach(string str in buttons)
    17.         {
    18.             buttonlist.Add(new Inputbutton(str));
    19.         }
    20.         var trigone = new Inputbutton("r2");
    21.         trigone.isTrigger = true;
    22.         var trigtwo = new Inputbutton("l2");
    23.         trigtwo.isTrigger = true;
    24.         buttonlist.Add(trigone);
    25.         buttonlist.Add(trigtwo);
    26.  
    27.  
    28.     }
    29.  
    30.     partial void UpdateInput()
    31.     {
    32.         foreach(Inputbutton buto in buttonlist)
    33.         {
    34.             buto.SetActualValue();
    35.         }
    36.  
    37.     }
    38.  
    39.     partial void FixedUpdateInput()
    40.     {
    41.      
    42.             foreach (Inputbutton buto in buttonlist)
    43.         {
    44.             if (Getbutton(buto.name, inputtype.down))
    45.             {
    46.                 foreach(InterfacePlayerAddon addon in addons)
    47.                 {
    48.                     addon.Button(buto.name, inputtype.down);
    49.                 }
    50.             }
    51.             buto.getchangedbyfixedupdate();
    52.         }
    53.      
    54.  
    55.     }
    56.  
    57.     public bool Getbutton(string name,inputtype type)
    58.     {
    59.         if(type== inputtype.normal)
    60.         {
    61.            // Debug.Log(buttonlist.Find(a => a.name == name).name + "(actual)" + buttonlist.Find(a => a.name == name).actualvalue.ToString());
    62.             return buttonlist.Find(a => a.name == name).actualvalue;
    63.  
    64.         }
    65.         else if (type == inputtype.down)
    66.         {
    67.             //Debug.Log(buttonlist.Find(a => a.name == name).name + "(down)" + buttonlist.Find(a => a.name == name).buttondown.ToString());
    68.             return buttonlist.Find(a => a.name == name).buttondown;
    69.  
    70.         }
    71.         else
    72.         {
    73.           //  Debug.Log(buttonlist.Find(a => a.name == name).name + "(up)" + buttonlist.Find(a => a.name == name).buttonup.ToString());
    74.  
    75.             return buttonlist.Find(a => a.name == name).buttonup;
    76.  
    77.         }
    78.     }
    79.     public enum inputtype
    80.     {
    81.         normal,
    82.         down,
    83.         up
    84.     }
    85. }
    86.  
    87.  
    88.  
    89. public class Inputbutton
    90. {
    91.     public string name;
    92.     public bool actualvalue;
    93.     public bool previousvalue;
    94.     public bool isTrigger;
    95.     public bool buttondown;
    96.     public bool buttonup;
    97.     public bool gotchangedByFixedInput;
    98.  
    99.     public Inputbutton(string name)
    100.     {
    101.         this.name = name;
    102.     }
    103.  
    104.     public void SetActualValue()
    105.     {
    106.         previousvalue = actualvalue;
    107.  
    108.  
    109.  
    110.  
    111.         if (!isTrigger)
    112.             actualvalue = CrossPlatformInputManager.GetButton(name + "button");
    113.         else
    114.             actualvalue = CrossPlatformInputManager.GetAxis(name + "trigger") > 0.1f;
    115.  
    116.         if (gotchangedByFixedInput)
    117.         {
    118.             if (!previousvalue & actualvalue)
    119.             {
    120.                 buttondown = true;
    121.             }
    122.             else if (previousvalue & !actualvalue)
    123.             {
    124.                 buttonup = true;
    125.             }
    126.         }
    127.     }
    128.  
    129.     public void getchangedbyfixedupdate()
    130.     {
    131.         buttondown = false;
    132.         buttonup = false;
    133.         gotchangedByFixedInput = true;
    134.     }
    135. }

    I don't know what your charging feature keycode is, is it "A" too ?
    I'm not sure i understood your problem, is the shoot animation called too early?
    Is the shoot animation still called after your character stopeed shooting?
     
    Last edited: Sep 25, 2017
    Oddinx likes this.
  3. Oddinx

    Oddinx

    Joined:
    Mar 2, 2017
    Posts:
    12
    Yeah the key code is A too and I'm doing a character that can charge his attack just like megaman, but the problem is that when I hold down the button to charge the attack my character stays in the shoot animation.
     
  4. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    In the animator , make the shooting animation to last for something like 0.2 seconds, and to be launched by a trigger.
    Instead of anim.SetBool, use anim.SetTrigger. Or maybe you can find a way to make the animator itself set the bool to false after some time.

    Another way of doing it would be to add a timer inside your script itself. (the code i will show you is a crappy code, find a better way of doing it , it's better in the animator)

    Code (CSharp):
    1. public float shootimer = 0;
    2.  
    3. void Update()
    4. {
    5. if(Input.GetKeyDown(chargeshoot)
    6. {
    7. //Instantiate the sutff;
    8. //if is on the ground
    9. shootimer = 0.2;
    10. }
    11.  
    12. if(shootimer >0)
    13. {
    14. anim.SetBool("shooting",true);
    15. shootimer -= Time.deltaTime;
    16. }
    17. else
    18. {
    19. anim.SetBool("shooting",false);
    20. }
    21. }
     
    Oddinx likes this.