- Home /
How to prevent an animation from triggering more than on due to fast clicking?
I am making a third person game and the player is allowed to pick up and throw a ball. The throwing animation is only allowed to trigger when the ball is a child of the player. the problem is that when you click the mouse button to fast the throwing animation will trigger again when the ball has already been thrown. Here is my script using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playerholdandthrow : MonoBehaviour {
//Variables for holding the ball
public Transform ball;
public Transform PosToSet;
public Rigidbody rb;
//Variables for throwing the ball
public Transform t_Camera; // Drag&Drop your camera over here from the inspector.
private Vector3 v3_Forcex; // Force reference vector.
private Vector3 v3_Forcey; // Force reference vector.
public float f_Multiplierx; // A multiplier value if the force wouldn't be enough.
public float f_Multipliery; // A multiplier value if the force wouldn't be enough.
//Variables for pick up and throwing animations
static Animator anim;
public Transform ballpick;
public Transform ballcatch;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
if (Time.deltaTime == 0f)
return;
if(ball.parent == PosToSet)
{
if (Input.GetMouseButtonDown(0))
{
anim.SetTrigger("isThrowing");
}
}
//Changes the ball's position for picking up
if(ball.parent == ballpick)
{
ball.position = ballpick.position;
(ball.GetComponent(typeof(Collider)) as Collider).isTrigger = true;
}
//Changes the ball's position for catching
if(ball.parent == ballcatch)
{
ball.position = ballcatch.position;
(ball.GetComponent(typeof(Collider)) as Collider).isTrigger = true;
}
if(ball.parent == PosToSet)
{
ball.GetComponent<Rigidbody>().useGravity = false;
ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
ball.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
ball.position = PosToSet.position;
ball.rotation = PosToSet.rotation;
ball.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
(ball.GetComponent(typeof(Collider)) as Collider).isTrigger = true;
}
}
//Plays pick up or catch animation when the player collides with the ball at the bottom or the top.
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "ball")
{
// turn to local position
Vector3 localContactPoint = col.contacts[0].point - transform.position;
// Assuming player position is at feet of player and player is 2 units tall
if (localContactPoint.y > 1f) { // above waist
// Debug.Log("Catch");
GetComponent<playercontrols>().enabled = false;
ball.SetParent (ballcatch.transform);
anim.SetTrigger("isCatching");
} else { // else below waist
// Debug.Log("Pick Up");
GetComponent<playercontrols>().enabled = false;
ball.SetParent (ballpick.transform);
anim.SetTrigger("isGrabbing");
}
}
}
//Functions for picking up and throwing
void pickupandcatch ()
{
ball.SetParent (PosToSet.transform);
}
void release ()
{
ball.GetComponent<Rigidbody>().useGravity = true;
(ball.GetComponent(typeof(Collider)) as Collider).isTrigger = false;
ball.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
ball.transform.parent = null;
}
void throwball ()
{
v3_Forcex = t_Camera.forward;
v3_Forcey = t_Camera.up;
ball.GetComponent<Rigidbody>().AddForce(v3_Forcex * f_Multiplierx, ForceMode.Force);
ball.GetComponent<Rigidbody>().AddForce(v3_Forcey * f_Multipliery, ForceMode.Force);
}
}
Answer by zingzingzingbah · Feb 08, 2019 at 12:37 AM
I'm perhaps not this most efficient programmer, but something like this would work:
if (Input.GetMouseButtonDown(0) && cooldownPeriod == false){
cooldownPeriod = true
// do mouse click stuff
...
...
}
then add this in Update():
if (cooldownPeriod == true) {
coolDownTimer += Time.deltaTime;
if(coolDownTimer > 30seconds){
cooldownPeriod = false
coolDownTimer = 0;
}
}
Answer by Chimer0s · Feb 08, 2019 at 03:24 AM
This may have to do with the animation controller. I'm assuming that the clip activated when "isThrowing" is set can be called from any state? If that's the case, there's a checkbox in the transition settings to the clip that can be unchecked called "Can Transition To Self." Unchecking this will prevent the clip from being activated again while it's already playing. Assuming Release() is called as an event of the animation, that should solve your problem.