Skip to content

Instantly share code, notes, and snippets.

@sabotai
Last active September 27, 2016 21:36
Show Gist options
  • Select an option

  • Save sabotai/d2d61675e128d454552a87524cf10f3b to your computer and use it in GitHub Desktop.

Select an option

Save sabotai/d2d61675e128d454552a87524cf10f3b to your computer and use it in GitHub Desktop.
GD205 F16 Week 5 In-Class
using UnityEngine;
using System.Collections;
public class Hangry : MonoBehaviour {
public GameObject fishPellet;
Vector3 destination;
public float howHungry;
bool hungry;
// Use this for initialization
void Start () {
hungry = true;
}
// Update is called once per frame
void Update () {
//the offset between these 2 positions will tell us which direction
//normalize will convert it into a direction with each position variable b/w -1 and 1
Vector3 direction = Vector3.Normalize (destination - transform.position);
Debug.Log ("fish velocity = " + GetComponent<Rigidbody>().velocity);
if (hungry){
destination = fishPellet.transform.position;
//hungry? add force in a direction multiplied by a speed
//hey you there... make sure you are able to code this on your own and not just copy/paste this in
GetComponent<Rigidbody> ().AddForce (direction * howHungry);
} else {
//not hungry? halt the rb velocity
GetComponent<Rigidbody>().velocity = Vector3.zero;
}
}
void OnCollisionEnter(Collision col){
//is the gameObject collided with the same as our fishPellet?
if (col.gameObject == fishPellet){
//destroy the object collided with now that we know it was the fishpellet
Destroy (col.gameObject);
//stop being hungry
hungry = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment