Skip to content
Permalink
0aa4d3e02a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
50 lines (35 sloc) 1.23 KB
using UnityEngine;
using System.Collections;
public class Archer : MonoBehaviour
{
public int ammo = 5;
public Transform target;//set target from inspector instead of looking in Update
public float speed = 1f;
public GameObject player = GameObject.Find("Player");
// public Vector2 playerSpot;
public Vector2 enemySpot;
public float distance;
public GameObject projectile;
public Transform player2;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//rotate to look at the player
transform.LookAt(target.position);
transform.Rotate(new Vector3(0, -90, 0), Space.Self);//correcting the original rotation
//move towards the player
if (Vector3.Distance(transform.position, target.position) > 3f)
{//move if distance from target is greater than 1
transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
if (ammo > 0)
{
GameObject spawn = Instantiate(projectile, new Vector2(transform.position.x, transform.position.y), Quaternion.Euler(0, 0, 0)) as GameObject;
ammo = ammo - 1;
}
}
}
}