This post contains all the code that’s been written from this YouTube video.
PlayGamesScript.cs
using UnityEngine; public class PlayerMovement : MonoBehaviour { bool wasJustClicked = true; bool canMove; Vector2 playerSize; Rigidbody2D rb; public Transform BoundaryHolder; Boundary playerBoundary; 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; } } // Use this for initialization void Start () { playerSize = GetComponent<SpriteRenderer>().bounds.extents; rb = GetComponent<Rigidbody2D>(); 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 ((mousePos.x >= transform.position.x && mousePos.x < transform.position.x + playerSize.x || mousePos.x <= transform.position.x && mousePos.x > transform.position.x - playerSize.x) && (mousePos.y >= transform.position.y && mousePos.y < transform.position.y + playerSize.y || mousePos.y <= transform.position.y && mousePos.y > transform.position.y - playerSize.y)) { 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; } } }
Mine keeps getting stuck in the middle
I have copied the code and still doesn’t change anything
I have followed all the steps
the Ai blue is not playing with me is only blocking the puck only.. help please