Commit db94b0c8 authored by Luke A Smith's avatar Luke A Smith

Merge branch 'enemyRanged' into 'master'

Ranged Enemy

See merge request !29
parents ee05c260 6a52a6fb
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyProjectile : MonoBehaviour {
float travelDistance; //how far it can go
Vector2 start; //the starting point of the projectile
public void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("collided");
if (!other.gameObject.CompareTag("Enemy"))
{
if (other.gameObject.CompareTag("Player"))
{
//damage player
}
Destroy(this.gameObject);
}
}
void Start()
{
start = transform.position;
}
void Update()
{
if (new Vector2(transform.position.x - start.x,
transform.position.y - start.y).magnitude > travelDistance) //if the projectile has travelled less than the max distance
{
Destroy(this.gameObject);
}
}
public void ProjectileExpiration(Vector2 velocity, float travelDistance)
{
GetComponent<Rigidbody2D>().velocity = velocity;
this.travelDistance = travelDistance;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyRanged : BaseEnemy
{
public Collider2D fieldOfView; //the 2D collider trigger on the enemy's child object checking if the player is in range to fire
public GameObject projectile; //the prefab of enemy projectiles
public float maxDistance; //the distance projectiles can travel;
public float projectileSpeed; //how fast the projectiles travel
public float fireCooldown; //the time between enemy attacks in units of seconds
float fireTimer;
public bool isFacingRight; //modifies the velocity of the fired projectile according to direction
public float angleOfSpread; //the greatest possible angle from the center projectiles can randomly spread; ******entered in radians*******
void Start ()
{
fireTimer = 0;
if (!isFacingRight) fieldOfView.gameObject.transform.localPosition = new Vector2(-fieldOfView.gameObject.transform.localPosition.x, fieldOfView.gameObject.transform.localPosition.y);
}
void Update ()
{
if (fieldOfView.IsTouching(GameObject.Find("Player").GetComponent<Collider2D>())) //if the player is in view
{
if (fireTimer <= 0)
{
Attack();
fireTimer = fireCooldown;
}
}
if(fireTimer > 0)
{
fireTimer -= Time.deltaTime;
}
}
void PositionFOV() //run to properly position the enemy's fov
{
}
void Attack()
{
float generatedAngle = Random.Range(-angleOfSpread,angleOfSpread);
Vector2 directionModifier = new Vector2(Mathf.Cos(generatedAngle), Mathf.Sin(generatedAngle)); //unit vector for projectile spread
GameObject projectileInstance = Instantiate(projectile,gameObject.transform);
projectileInstance.transform.position = transform.position;
if (!isFacingRight)
{
projectileInstance.GetComponent<enemyProjectile>().ProjectileExpiration(new Vector2(-projectileSpeed * directionModifier.x, projectileSpeed * directionModifier.y), maxDistance);
}
else
{
projectileInstance.GetComponent<enemyProjectile>().ProjectileExpiration(new Vector2(projectileSpeed * directionModifier.x, projectileSpeed * directionModifier.y), maxDistance);
}
// StartCoroutine(ProjectileExpiration(projectileInstance, projectileInstance.transform.position));
//coroutine uses the instance of the projectile and its starting position before moving
}
/*IEnumerator ProjectileExpiration(GameObject projectileInstance, Vector3 start) //coroutine which runs to check the projectile travels only its max distance
{
while(new Vector2(projectileInstance.transform.position.x - start.x,
projectileInstance.transform.position.y - start.y).magnitude < maxDistance) //while the projectile has travelled less than the max distance
{
yield return null;
}
Destroy(projectileInstance);
}*/
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment