This post contains all the code that’s been written in this YouTube video.
ScoreScript.cs
using UnityEngine;
using UnityEngine.UI;
public class ScoreScript : MonoBehaviour {
public enum Score
{
AiScore, PlayerScore
}
public Text AiScoreTxt, PlayerScoreTxt;
private int aiScore, playerScore;
public void Increment(Score whichScore)
{
if (whichScore == Score.AiScore)
AiScoreTxt.text = (++aiScore).ToString();
else
PlayerScoreTxt.text = (++playerScore).ToString();
}
}
PuckScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PuckScript : MonoBehaviour {
public ScoreScript ScoreScriptInstance;
public static bool WasGoal { get; private set; }
private Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
WasGoal = false;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (!WasGoal)
{
if (other.tag == "AiGoal")
{
ScoreScriptInstance.Increment(ScoreScript.Score.PlayerScore);
WasGoal = true;
StartCoroutine(ResetPuck());
}
else if (other.tag == "PlayerGoal")
{
ScoreScriptInstance.Increment(ScoreScript.Score.AiScore);
WasGoal = true;
StartCoroutine(ResetPuck());
}
}
}
private IEnumerator ResetPuck()
{
yield return new WaitForSecondsRealtime(1);
WasGoal = false;
rb.velocity = rb.position = new Vector2(0, 0);
}
}
PlayerMovement.cs
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
bool wasJustClicked = true;
bool canMove;
Rigidbody2D rb;
public Transform BoundaryHolder;
Boundary playerBoundary;
Collider2D playerCollider;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
playerCollider = GetComponent<Collider2D>();
playerBoundary = new Boundary(BoundaryHolder.GetChild(0).position.y,
BoundaryHolder.GetChild(1).position.y,
BoundaryHolder.GetChild(2).position.x,
BoundaryHolder.GetChild(3).position.x);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (wasJustClicked)
{
wasJustClicked = false;
if(playerCollider.OverlapPoint(mousePos))
{
canMove = true;
}
else
{
canMove = false;
}
}
if (canMove)
{
Vector2 clampedMousePos = new Vector2(Mathf.Clamp(mousePos.x, playerBoundary.Left,
playerBoundary.Right),
Mathf.Clamp(mousePos.y, playerBoundary.Down,
playerBoundary.Up));
rb.MovePosition(clampedMousePos);
}
}
else
{
wasJustClicked = true;
}
}
}
AiScript.cs
using UnityEngine;
public class AiScript : MonoBehaviour {
public float MaxMovementSpeed;
private Rigidbody2D rb;
private Vector2 startingPosition;
public Rigidbody2D Puck;
public Transform PlayerBoundaryHolder;
private Boundary playerBoundary;
public Transform PuckBoundaryHolder;
private Boundary puckBoundary;
private Vector2 targetPosition;
private bool isFirstTimeInOpponentsHalf = true;
private float offsetXFromTarget;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
startingPosition = rb.position;
playerBoundary = new Boundary(PlayerBoundaryHolder.GetChild(0).position.y,
PlayerBoundaryHolder.GetChild(1).position.y,
PlayerBoundaryHolder.GetChild(2).position.x,
PlayerBoundaryHolder.GetChild(3).position.x);
puckBoundary = new Boundary(PuckBoundaryHolder.GetChild(0).position.y,
PuckBoundaryHolder.GetChild(1).position.y,
PuckBoundaryHolder.GetChild(2).position.x,
PuckBoundaryHolder.GetChild(3).position.x);
}
private void FixedUpdate()
{
if (!PuckScript.WasGoal)
{
float movementSpeed;
if (Puck.position.y < puckBoundary.Down)
{
if (isFirstTimeInOpponentsHalf)
{
isFirstTimeInOpponentsHalf = false;
offsetXFromTarget = Random.Range(-1f, 1f);
}
movementSpeed = MaxMovementSpeed * Random.Range(0.1f, 0.3f);
targetPosition = new Vector2(Mathf.Clamp(Puck.position.x + offsetXFromTarget, playerBoundary.Left,
playerBoundary.Right),
startingPosition.y);
}
else
{
isFirstTimeInOpponentsHalf = true;
movementSpeed = Random.Range(MaxMovementSpeed * 0.4f, MaxMovementSpeed);
targetPosition = new Vector2(Mathf.Clamp(Puck.position.x, playerBoundary.Left,
playerBoundary.Right),
Mathf.Clamp(Puck.position.y, playerBoundary.Down,
playerBoundary.Up));
}
rb.MovePosition(Vector2.MoveTowards(rb.position, targetPosition,
movementSpeed * Time.fixedDeltaTime));
}
}
}

After finish this tutorial , i got this error in the unity console when try to check the (play mode ) all compiler error have to be fixed before enter the play mode
and error appear like this ( Assets/Art/Script/AiScript.cs(14,13): error CS0246: The type or namespace name `Boundary’ could not be found. Are you missing `UnityEngine.Experimental.VR’ using directive?)
any solution for that ??
the red player cant score… please help.. only Ai blue can score
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your article helped me a lot, is there any more related content? Thanks!
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://accounts.binance.com/ES_la/register?ref=VDVEQ78S