Created
October 21, 2021 11:35
-
-
Save TheCuttlefish/f9ab7a2fd3e41ddcff9b71352bc06054 to your computer and use it in GitHub Desktop.
Pool Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Aim : MonoBehaviour | |
{ | |
Vector3 mousePos; | |
public GameObject whiteBall; | |
public float dist;//white ball to mouse pos | |
void Update() | |
{ | |
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition) + new Vector3(0,0,10); | |
transform.position = mousePos; | |
transform.up = (transform.position - whiteBall.transform.position); | |
dist = Vector2.Distance(mousePos, whiteBall.transform.position)/10; | |
transform.localScale = new Vector3(0.1f, dist, 1); | |
} | |
public Vector2 GetDirection() | |
{ | |
return (transform.position - whiteBall.transform.position); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Hole : MonoBehaviour | |
{ | |
private void OnTriggerEnter2D(Collider2D collision) | |
{ | |
if(collision.tag == "Ball") | |
{ | |
if (collision.name == "white ball") | |
{ | |
collision.GetComponent<WhiteBall>().Reset(); | |
} | |
else | |
{ | |
collision.gameObject.SetActive(false); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class WhiteBall : MonoBehaviour | |
{ | |
public GameObject aim; | |
public bool isReadyShoot = false; | |
Vector2 shootingDir; | |
Vector2 originalPos; | |
private void Start() | |
{ | |
originalPos = transform.position; | |
} | |
public void Reset() | |
{ | |
GetComponent<Rigidbody2D>().velocity = Vector2.zero; | |
transform.position = originalPos; | |
} | |
private void OnMouseDown() | |
{ | |
aim.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);//show aim | |
isReadyShoot = true; | |
} | |
void Update() | |
{ | |
if (Input.GetMouseButtonUp(0)){ | |
if (isReadyShoot == true) | |
{ | |
aim.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);//hide aim | |
isReadyShoot = false; | |
shootingDir = aim.GetComponent<Aim>().GetDirection(); | |
GetComponent<Rigidbody2D>().AddForce(shootingDir * 200f); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment