Click here to Skip to main content
15,915,509 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, the problem is if I click once on left mouse button there plays attack animation and my character performs an attack but if I click more than one time on left mouse button the character just freezes and stops moving but it is possible to rotate it. Here is the script I have attached to my character.
using UnityEngine;
using System.Collections;

public class PlayerShot : MonoBehaviour {
	public GameObject Player;
	
	
	void Start(){
		
	}
	
	void Update(){
         
            if (Input.GetMouseButtonDown(0))
            {
                Player.GetComponent<Animator>().SetTrigger("Attack");

            }

            else if (Input.GetMouseButtonUp(0))
            {
                Player.GetComponent<Animator>().SetTrigger("Idle");

            }
        }
		
	}


What I have tried:

I have tried to change the code but no result
Posted
Updated 9-Jan-18 3:53am

1 solution

You could try to add a private field holding when the last mouse event happened. This way, you can prevent method from executing if not enough time passed since last invocation.
Something like:
C#
using UnityEngine;
using System.Collections;

public class PlayerShot : MonoBehaviour
{
   public GameObject Player;

   private const float WAIT = 0.1f; // 1/10th of a second

   private float lastClik = 0f;
	
   void Update() {

      if (Time.time - lastClick > WAIT) {

         lastClick = Time.time;

         if (Input.GetMouseButtonDown(0))
         {
            Player.GetComponent<Animator>().SetTrigger("Attack");
         }
         else if (Input.GetMouseButtonUp(0))
         {
            Player.GetComponent<Animator>().SetTrigger("Idle");
         }
      }
   }
}

Kindly.
 
Share this answer
 
Comments
Mir Usmanov 9-Jan-18 10:39am    
Thank you for your answer phil.o but the same as before, that is, if I continuously click the button, my player again freezes maybe animation cannot catch up with the click or something like that.
phil.o 9-Jan-18 10:56am    
Yes, probably. This tutorial is quite interesting and, while not dealing only with your actual issue, may content some useful informations and tricks:
Merry Fragmas 3.0: Multiplayer FPS Foundation[^]
Mir Usmanov 15-Jan-18 8:12am    
All right phil.o thanks a lot
phil.o 15-Jan-18 8:46am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900