STICK FLICK PENALTY KICK GAME 4: GOALKEEPER MOVEMENT

The one thing that we are badly missing in our Penalty Kick Game is the movement of our Goalkeeper. What good is having the goalkeeper if he doesn't move? I've had enough of it. Let's make the keeper save some penalties now.

What can you achieve at the end of this post?


To begin, let's create a new C# Script under the Scripts folder and name it as GoalieAnimation. We will attach it the Goalie gameobject which is the child of keeper gameobject.
Drag and drop the Football object from the Hierarchy to the GoalieAnimation script's swipe field in the inspector.


Double click the script to open it and add the below variables

private Vector3 fp;   //First finger position
private Vector3 lp;   //Last finger position
private bool isKicked;  //flag to indicate if the ball has been kicked
public SwipeControl swipe;   //reference to SwipeControl script
private float dragDistance;  //Distance needed for a swipe to register

In the Start function we will set the dragDistance to 5% of the screenwidth

void Start(){
dragDistance = Screen.width*5/100;  //set dragdistance to 5% of screen width
}

The Update function will look something like

void Update(){                
isKicked = swipe.isKickedPlayer;  //check if the ball is kicked
if(swipe.turn==1)     //check if players turn to keep the goal
playerGoalie();     
else if(swipe.turn==0)    //check if opponent's turn to keep
opponentGoalie();           
}

The Update function above will check whose turn it is to keep the goal and calls the respective function accordingly.

In the opponentGoalie function we will generate a random number ranging from 1 to 3 and assign an animation to play for each number.
(Note that we have three animations created already which were made available in the package in our very first post. We will perhaps do a post on how to use the unity animation tool to animate the character.)
We will be using the legacy animation system as it is more than sufficient for what we want to achieve.

void opponentGoalie(){                
  //check if the ball is kicked so that we can play the animation
  if(isKicked){                      
   int num = (int)Random.Range(1f, 3f); //generate a random number within 1 to 3
   //play a particular animation for a particular number generated
   if(num==1){
    animation.Play("RightSave");
   }
   else if(num==2){
    animation.Play("LeftSave");
   }
   else if(num==3){
    animation.Play("StandSave");
   }
   swipe.isKickedPlayer = false;  //once the animation is played set the isKicked flag to false
  }
 }

As clearly mentioned in the comments, we check the isKicked flag, generate random number in the range 1 to 3. Play the respective animation and set the isKicked flag to false so as to make sure that the animation is not played again. Simple enough!

There is one thing left to code, which is the player controlling the goalie animation.

We will check If it's the opponent's turn to keep the goal, if so then we see if there's a tap/swipe detected on the screen. Once the tap is detected we will add force to the ball after a delay of 0.2 seconds (This was covered in the previous post). Now we will play the animation based on the swipe direction. What this means is we swipe and animate the goalie and correspondingly the ball is shot after the swipe is detected. So, it's like one throw and two rewards.

Now, we have two options to check if the screen is swiped, create a new function altogether or use the playerLogic function of the swipeControl script.
Just for the sake of simplicity we will create a new function to play the animations based on the swipe direction.

void playerGoalie(){
  
  foreach (Touch touch in Input.touches)
  {
   if (touch.phase == TouchPhase.Began)
   {
    fp = touch.position;
    lp = touch.position;
   }
   
   if ((touch.phase == TouchPhase.Ended) && swipe.isKickedOpponent)
   {
    
    lp = touch.position;
    
    //First check if it's actually a drag
    if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
    {   //It's a drag
     
     //Now check what direction the drag was
     //First check which axis
     if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
     {   //If the horizontal movement is greater than the vertical movement...
      if ((lp.x > fp.x) )  //If the movement was to the right)
      {   //Right move
       animation.Play("RightSave");
       
      }
      else if((lp.x < fp.x)  )
      {   //Left move
       animation.Play("LeftSave");
       
      }
     }
     else
     {   //the vertical movement is greater than the horizontal movement
      if ((lp.y > fp.y))  //If the movement was up
      {   //Up move
       animation.Play("StandSave");
       
       
      }
     }
    }
   }
   
  }
  
 }

The above function is quite simple as we have seen it earlier. It adds the RightSave animation if we swipe to the right, LeftSave, if swiped to the left and StandSave, if we swipe up.

The complete GoalieAnimation script will look like:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GoalieAnimation : MonoBehaviour {
 
 private Vector3 fp;   //First finger position
 private Vector3 lp;   //Last finger position
 private bool isKicked;  //flag to indicate if the ball has been kicked
 public SwipeControl swipe;   //reference to SwipeControl script
 private float dragDistance;  //Distance needed for a swipe to register
 
 void Start(){
  dragDistance = Screen.width*5/100;  //set dragdistance to 5% of screen width
 }
 
 void Update(){                
  isKicked = swipe.isKickedPlayer;  //check if the ball is kicked
  if(swipe.turn==1)     //check if players turn to keep the goal
   playerGoalie();     
  else if(swipe.turn==0)    //check if opponent's turn to keep
   opponentGoalie();           
 }
 
 void opponentGoalie(){                
  //check if the ball is kicked so that we can play the animation
  if(isKicked){                      
   int num = (int)Random.Range(1f, 3f); //generate a random number within 1 to 3
   //play a particular animation for a particular number generated
   if(num==1){
    animation.Play("RightSave");
   }
   else if(num==2){
    animation.Play("LeftSave");
   }
   else if(num==3){
    animation.Play("StandSave");
   }
   swipe.isKickedPlayer = false;  //once the animation is played set the isKicked flag to false
  }
 }
 
 void playerGoalie(){
  
  foreach (Touch touch in Input.touches)
  {
   if (touch.phase == TouchPhase.Began)
   {
    fp = touch.position;
    lp = touch.position;
   }
   
   if ((touch.phase == TouchPhase.Ended) && swipe.isKickedOpponent)
   {
    
    lp = touch.position;
    
    //First check if it's actually a drag
    if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
    {   //It's a drag
     
     //Now check what direction the drag was
     //First check which axis
     if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
     {   //If the horizontal movement is greater than the vertical movement...
      if ((lp.x > fp.x) )  //If the movement was to the right)
      {   //Right move
       animation.Play("RightSave");
       
      }
      else if((lp.x < fp.x)  )
      {   //Left move
       animation.Play("LeftSave");
       
      }
     }
     else
     {   //the vertical movement is greater than the horizontal movement
      if ((lp.y > fp.y))  //If the movement was up
      {   //Up move
       animation.Play("StandSave");
       
       
      }
     }
    }
   }
   
  }
  
 }
}


Play to test this game, of course with Unity Remote app opened and your device connected to the computer.

See you around.

Stick Flick Penalty Kick Game 1: The Football Ground
Stick Flick Penalty Kick Game 2: Let's Shoot The Football
Stick Flick Penalty Kick Game 3: Add GoalLine Technology 
Stick Flick Penalty Kick Game 5: Who Is The Winner?
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
Connect with him on:
    Blogger
    Facebook

5 comments:

  1. how can shoot the ball. i not know , use botton

    ReplyDelete
    Replies
    1. You got to swipe in the direction you want to shoot...

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. i cant drag the football to the inspector

    ReplyDelete