#4 Make an Air Hockey Game in Unity – ARTIFICIAL INTELLIGENCE (AI) (Code)

1  comments

This post contains all the code that’s been written in this YouTube video.

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()
    {
        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));
    }
}

 

Boundary.cs

struct Boundary
{
    public float Up, Down, Left, Right;

    public Boundary(float up, float down, float left, float right)
    {
        Up = up; Down = down; Left = left; Right = right;
    }
}

 

About the author 

Matt Rešetár

Matt is an app developer with a knack for teaching others. Working as a freelancer and most importantly developer educator, he is set on helping other people succeed in their Flutter app development career.

You may also like

  • Assets/Scripts/AiScript.cs(12,50): error CS0723: `AiScript.playerBoundary’: cannot declare variables of static types

    Assets/Scripts/AiScript.cs(12,50): error CS0723: `AiScript.puckBoundary’: cannot declare variables of static types

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >