Permalink
Cannot retrieve contributors at this time
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?
Intro_Scritping/Roguelike2d/Assets/Script/Archer.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (35 sloc)
1.23 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |