Skip to content

Instantly share code, notes, and snippets.

@radiatoryang
Last active March 31, 2016 19:28
Show Gist options
  • Save radiatoryang/b01637f7efbc5e2871a218fc13b68896 to your computer and use it in GitHub Desktop.
Save radiatoryang/b01637f7efbc5e2871a218fc13b68896 to your computer and use it in GitHub Desktop.
Cat and Mouse lab base
using UnityEngine;
using System.Collections;
public class Cat : MonoBehaviour {
// declare a public variable, of type Transform, called "mouse"
public Transform mouse;
bool shouldIPlayASound = false;
void FixedUpdate () {
// declare a var of type Vector3, called "directionToMouse", set to a vector that goes from [current position] to [mouse's current position]
Vector3 directionToMouse = mouse.position - transform.position;
// if the angle between [current forward direction] vs. [directionToMouse] is less than 90 degrees, then...
if ( Vector3.Angle ( transform.forward, directionToMouse ) < 90f ) {
// declare a var of type Ray, called "catRay" that starts from [current position] and goes toward [mouse's current position]
Ray catRay = new Ray( transform.position, directionToMouse );
// declare a var of type RaycastHit, called "catRayHitInfo"
RaycastHit catRayHitInfo = new RaycastHit();
// if raycast with catRay and catRayHitInfo for 100 units is TRUE...
if ( Physics.Raycast ( catRay, out catRayHitInfo, 100f ) ) {
// if catRayHitInfo.collider.tag is exactly equal to the word "Mouse"...
if ( catRayHitInfo.collider.tag == "Mouse" ) {
// the cat can see the mouse, play a sound
if ( GetComponent<AudioSource>().isPlaying == false && shouldIPlayASound == false ) {
GetComponent<AudioSource>().Play ();
shouldIPlayASound = true;
}
Debug.DrawRay ( catRay.origin, catRay.direction * 100f, Color.yellow );
// if catRayHitInfo.distance is less than or equal to 5...
if ( catRayHitInfo.distance <= 5f ) {
// then destroy the mouse object (we caught the mouse!)
Destroy ( mouse.gameObject );
} else {
// move toward the mouse!
GetComponent<Rigidbody>().AddForce (directionToMouse.normalized * 1000f);
}
} else {
shouldIPlayASound = false; // reset sound variable
}
}
}
}
}
using UnityEngine;
using System.Collections;
public class Mouse : MonoBehaviour {
//declare a public variable, of type Transform, called "cat"
public Transform cat;
void FixedUpdate () {
// declare a var of type Vector3, called "directionToCat", set to a vector that goes from [current position] to [cat's current position]
Vector3 directionToCat = cat.position - transform.position;
// if the angle between [current forward direction] vs. [directionToCat] is less than 180 degrees, then...
if ( Vector3.Angle ( transform.forward, directionToCat ) < 180f ) {
// declare a var of type Ray, called "mouseRay" that starts from [current position] and goes toward [cat's current position]
Ray mouseRay = new Ray( transform.position, directionToCat );
// declare a var of type RaycastHit, called "mouseRayHitInfo"
RaycastHit mouseRayHitInfo = new RaycastHit();
// if raycast with mouseRay and mouseRayHitInfo for 100 units is TRUE, then...
if ( Physics.Raycast ( mouseRay, out mouseRayHitInfo, 100f ) ) {
// if mouseRayHitInfo.collider.tag is exactly equal to the word "Cat"...
if ( mouseRayHitInfo.collider.tag == "Cat" ) {
Debug.DrawRay ( mouseRay.origin, mouseRay.direction * 100f, Color.cyan );
// run away from the cat!
GetComponent<Rigidbody>().AddForce ( (-directionToCat.normalized-transform.forward) * 1000f);
}
}
}
}
}
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
// Update is called once per frame
void FixedUpdate () {
// Add a physics force going forward (to always move forward)
GetComponent<Rigidbody>().velocity = transform.forward * 10f + Physics.gravity;
// declare a var of type Ray, called "moveRay" that starts from [current position] and goes [current forward direction]
Ray ray = new Ray( transform.position, transform.forward );
// if Raycast with moveRay for 3 unit is TRUE...
if ( Physics.SphereCast ( ray, 0.5f, 3f ) ) {
// then randomly turn 90 degrees left or right (around Y axis)
transform.Rotate (0f, Random.value > 0.5 ? -90f : 90f, 0f );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment