I’m developing a flick goal game in Unity where the player character’s animation needs to run, pause, and resume based on specific mouse events. Here’s what I want to achieve:
-
The animation should pause when the player presses the left mouse button (Input.GetMouseButtonDown(0)).
-
The animation should wait for the player to drag the mouse.
-
The animation should continue (i.e., the player hits the ball) when the mouse button is released after dragging.
I have an Animator component attached to my player character and a ball object that needs to be hit. How can I implement this animation control logic in a script?
Here’s what I have so far:
if(_isShooting) { // neu banh chua vao luoi hoac trung thu mon, khung thanh thi banh duoc phep bay voi van toc dang co
if( _enableTouch && !_isInTutorial ) {
if(Input.GetMouseButtonDown(0)) { // touch phase began
mouseBegin(Input.mousePosition);
_playerController.OnMouseDownAnimationStart();
}
else if( Input.GetMouseButton(0) ) {
mouseMove(Input.mousePosition);
}
else if(Input.GetMouseButtonUp(0)) { // touch ended
mouseEnd();
}
}
if(_isShoot) {
Vector3 speed = _ballParent.InverseTransformDirection(_ball.velocity);
speed.z = _zVelocity;
_ball.velocity = _ballParent.TransformDirection(speed);
}
}
public void OnMouseDownAnimationStart()
{
// Handle the start of the animation
animator.speed = 0;
}