Saturday, 22 March 2014

Enemy movement due to player interaction

public class WindowWalker : MonoBehaviour {

public AudioClip HeartBeat;
// this is an object, so that you can move it around in the editor.
public Transform target;
public GameObject collect;
//public GameObject enemy;

// units per second
public float speed = 3;

//void Start()
//{
// gameObject.renderer.enabled = false;
//}

void Update(){

//if (collect != false)
//{
// gameObject.renderer.enabled = false;
//}

if (collect != true)
{
audio.PlayOneShot(HeartBeat, 1.0f);
//gameObject.renderer.enabled = true;
//spawn = (GameObject)Instantiate(Resources.Load("DarkEnemy"));
// remember, 10 - 5 is 5, so target - position is always your direction.
Vector3 dir = target.position - transform.position;

// magnitude is the total length of a vector.

// getting the magnitude of the direction gives us the amount left to move

float dist = dir.magnitude;

// this makes the length of dir 1 so that you can multiply by it.

dir = -dir.normalized;

// the amount we can move this frame

float move = speed * Time.deltaTime;

// limit our move to what we can travel.

if (move > dist)

move = dist;

// apply the movement to the object.

transform.Translate (dir * move);

Destroy(gameObject, 6);


}
}

//public GameObject Battery;
//public GameObject Man;
//public GameObject GameObjectA;
//public GameObject GameObjectB;

// Update is called once per frame
//void Update ()
//{
// if (Battery != true)
// {
// Instantiate(Man);
// MoveObject(GameObjectA, GameObjectB);
// Destroy(Man);
// }
//}
}

No comments:

Post a Comment