Skip to content

Instantly share code, notes, and snippets.

@overf10w
Last active December 2, 2017 02:38
Show Gist options
  • Select an option

  • Save overf10w/49f561f1ad5a3152db75c07d6e44ebe3 to your computer and use it in GitHub Desktop.

Select an option

Save overf10w/49f561f1ad5a3152db75c07d6e44ebe3 to your computer and use it in GitHub Desktop.
Shit
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DogController : MonoBehaviour
{
public PlayerController playerController;
public Transform ball;
public Transform player;
private bool m_FacingRight = true; // For determining which way the player is currently facing.
private bool isBallPicked = false;
private IEnumerator coroutine;
private IEnumerator goToPlayer;
public float speed = 1f;
private float radiusToBall = 2f;
private float radiusToPlayer = 3.3f;
private Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics.
// Use this for initialization
void Start()
{
playerController = FindObjectOfType<PlayerController>();
// ball =
playerController.commandDogReturn += OnCommandDogReturn;
playerController.commandDogBark += OnCommandDogBark;
playerController.commandDogFakeDeath += OnCommandDogFakeDeath;
playerController.commandDogFetch += OnCommandDogFetch;
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
CheckCollisions();
}
void OnCommandDogReturn()
{
Debug.Log("I return");
Flip();
}
void OnCommandDogBark()
{
Debug.Log("I bark!");
}
void OnCommandDogFakeDeath()
{
Debug.Log("I fake death!");
}
void OnCommandDogFetch()
{
Debug.Log("I fetch!");
}
// MOVEMENT
private void Flip()
{
// Switch the way the player is labelled as facing.
m_FacingRight = !m_FacingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void FetchBall()
{
}
// ASSISTANCE
void CheckCollisions()
{
Ray ray = new Ray(transform.position, transform.right);
Debug.DrawRay(transform.position, transform.right * 20f, Color.red);
Physics2D.queriesStartInColliders = false;
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right, 20f);
// Debug.Log(hit.transform.name);
if (hit.transform.tag == "ball" && isBallPicked == false)
{
coroutine = GoToPoint(hit.transform);
StartCoroutine(coroutine);
}
else
{
goToPlayer = GoToPlayer(player);
StopCoroutine(coroutine);
StartCoroutine(goToPlayer);
}
}
IEnumerator GoToPoint(Transform point)
{
while (transform.position.x < point.position.x)
{
transform.position = new Vector2(transform.position.x + 0.1f, transform.position.y);
if (Mathf.Abs(transform.position.x - point.position.x) < radiusToBall)
{
isBallPicked = true;
}
yield break;
}
}
IEnumerator GoToPlayer(Transform point)
{
while (true)
{
transform.position = new Vector2(transform.position.x - 0.1f, transform.position.y);
Debug.Log("Dog : " + transform.position.x + "Player : " + point.position.x + "Dog.x - Player.x : " + (transform.position.x - point.position.x).ToString());
if (Mathf.Abs(transform.position.x - point.position.x) < radiusToPlayer)
{
Debug.Log("Triggered!");
isBallPicked = false;
}
yield break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment