//2D movement in 3D environment
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GroundUnitMovement : MonoBehaviour {
//General Variables
public GameObject ammo;
private List<GameObject> Targets = new List<GameObject>();
public float scale_left = 0;
public float scale_right = 0;
public GameObject sprite = null;
//Control Variables
public float attack_Range = 0;
public float var_AutoX = 0;
public float speed;
private Rigidbody rigidbody_val = null;
private bool onGround = false;
void Start() {
//Set rigidbody
this.rigidbody_val = this.GetComponent<Rigidbody>();
}
void Update() {
//Check for destroyed player objects in list
this.UpdateTargetList();
//Check for player objects in radius
this.CheckSurrounding();
//all targets are located at x-coord: 0. Replace 0 with Target[0].position.x if needed.
if (Targets.Count == 0)
{
if (this.transform.position.x > 0)
{
this.var_AutoX = -this.speed;
//this.sprite.transform.rotation.eulerAngles.Set(0, scale_left, 0);
this.sprite.transform.rotation = new Quaternion(0, scale_left, 0, 1);
Debug.Log(this.sprite.transform.rotation);
}
if (this.transform.position.x < 0)
{
this.var_AutoX = this.speed;
this.sprite.transform.rotation = new Quaternion(0, scale_right, 0, 1);
//this.sprite.transform.rotation.eulerAngles.Set(0, scale_right, 0);
Debug.Log(this.sprite.transform.rotation);
}
//Movement is only allowed when the character is on the ground and has not targets in sight.
if (this.onGround)
{
this.rigidbody_val.AddForce(new Vector3(this.var_AutoX, 0), ForceMode.Impulse);
}
}
else
{
//Attack first unit in the list - The first encountered player object and or player.
//Will not target the player when he/she stands before the object but can attack it cause of the bullets passing him/her.
//create projectile.
Fire(Targets[0].transform.position);
}
}
//Creates a circular collider and records all entities within this circle.
void CheckSurrounding()
{
Collider[] hitTargets = Physics.OverlapSphere(this.transform.position, attack_Range);
Debug.DrawLine(this.transform.position, this.transform.position + new Vector3(attack_Range, 0), Color.blue);
Debug.DrawLine(this.transform.position, this.transform.position + new Vector3(-attack_Range, 0), Color.blue);
foreach (Collider c in hitTargets)
{
//When the object in the collider doesn't exist add to the target list.
if (c.tag == "Player" || c.tag == "playerObj")
{
if (!Targets.Contains(c.gameObject))
{
Targets.Add(c.gameObject);
}
}
}
}
void Fire(Vector3 target)
{
//Rotate firing object towards target.
Quaternion rotation = Quaternion.FromToRotation(this.transform.position, target);
//Create new instance of object.
GameObject.Instantiate(ammo, this.transform.position, rotation);
}
void UpdateTargetList()
{
List<GameObject> newList = new List<GameObject>();
foreach(GameObject go in this.Targets)
{
//if not destoyed add to the temp list.
if(go != null)
{
newList.Add(go);
}
}
if(this.Targets.Count != newList.Count)
{
this.Targets = newList;
}
}
void FixedUpdate()
{
//Check if unit is on the ground
this.IsOnGround();
}
//Requires a surface with the tag floor to work.
void IsOnGround()
{
//always false unless unit collides with floor;
this.onGround = false;
Ray ray_floor = new Ray(this.transform.position,
-this.transform.up);
RaycastHit hit_floor;
Debug.DrawRay(this.transform.position,
-this.transform.up);
if (Physics.Raycast(ray_floor,
out hit_floor,
(this.transform.lossyScale.y / 2 + 0.05f)))
{
if (hit_floor.transform.tag == "floor")
{
this.onGround = true;
}
}
}
}