Skip to content
Permalink
84c7f1111b
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
199 lines (162 sloc) 4.54 KB
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider))]
public class EnemyPhysics : MonoBehaviour {
public LayerMask collisionMask;
public bool hasPowerup= false;
private BoxCollider colliderX;
private Vector3 s;
private Vector3 c;
public float EnemySpeed;
public float gravity;
private bool offScreen = false;
private string lastHit;
private float skin = .005f;
private float dirX;
private float dirY;
private float direction = -1;
private Vector3 movement;
public GameObject PowerupPrefabz;
public GameObject bullet;
public bool isFlying = false;
public float shootOffset = 0;
public float shootSpeedN;
private Vector3 spawnPoint;
private float shootSpeed;
private bool closeToCam = false;
private Vector3 originalSize;
private Vector3 originalCenter;
private float colliderScaleX;
public AudioClip sound;
private bool dead = false;
[HideInInspector]
public bool grounded;
[HideInInspector]
public bool movementStopped;
Ray rayV;
Ray rayD;
Ray rayH;
RaycastHit hit;
void Start(){
colliderX = GetComponent<BoxCollider>();
s = colliderX.size;
c = colliderX.center;
colliderScaleX = transform.localScale.x;
SetCollider (s, c);
shootSpeed = shootSpeedN/100;
spawnPoint = transform.position;
Invoke ("startFiring", shootOffset);
}
public void move(Vector2 moveAmt) {
float deltaY = moveAmt.y *Time.deltaTime;
float deltaX = moveAmt.x;
Vector2 p = transform.position;
grounded= false;
for (int i = 0; i <3; i++) {
dirY = Mathf.Sign (deltaY);
float x = (p.x + c.x - s.x/2) + s.x/2 * i;
float y = p.y + c.y +s.y/2 * dirY;
rayV = new Ray(new Vector2(x,y), new Vector2(0,dirY));
Debug.DrawRay (rayV.origin, rayV.direction);
if (Physics.Raycast (rayV,out hit, Mathf.Abs (deltaY) + skin, collisionMask)) {
float dst = Vector3.Distance (rayV.origin, hit.point);
if (dst > skin) {
deltaY = -dst - skin * dirY;
}
else{
deltaY = 0;
}
grounded = true;
break;
}
}
movementStopped = false;
for (int i = 0; i <3; i++) {
dirX = Mathf.Sign (deltaX);
float x = p.x + c.x + s.x/2 * dirX;
float y = (p.y + c.y - s.y/2) + s.y/2 * i;
rayH = new Ray(new Vector2(x,y), new Vector2(dirX,0));
Debug.DrawRay (rayH.origin, rayH.direction);
if (Physics.Raycast (rayH,out hit, Mathf.Abs (deltaX) + skin, collisionMask)) {
float dst = Vector3.Distance (rayH.origin, hit.point);
if (dst > skin) {
deltaX = dst * dirX - skin * dirX;
}
else{
//deltaX = 0;
}
movementStopped = true;
break;
}
}
if (!grounded && !movementStopped) {
Vector3 playerDir = new Vector3(deltaX,deltaY);
Vector3 o = new Vector3((p.x + c.x + s.x/2 * dirX),(p.y + c.y +s.y/2 * dirY));
Debug.DrawRay (o, playerDir.normalized);
rayD = new Ray(o, playerDir.normalized);
if(Physics.Raycast (rayD,Mathf.Sqrt (deltaX*deltaX + deltaY*deltaY),collisionMask)){
grounded = true;
deltaY = 0;
}
}
Vector2 finalTransform = new Vector2 (deltaX,deltaY);
if (offScreen){
transform.Translate (new Vector2 (moveAmt.x, deltaY), Space.World);
}
else{
transform.Translate(finalTransform, Space.World);
}
}
void OnTriggerEnter(Collider other) {
lastHit = other.tag;
if (lastHit =="Backboard"){
//Debug.Log (lastHit);
}
else if (lastHit=="Weapon"){
audio.PlayOneShot (sound);
CancelInvoke ("spawnBullet");
renderer.enabled = false;
Invoke("selfDestruct", sound.length );
dead=true;
}
}
void selfDestruct(){
if (hasPowerup){
Instantiate (PowerupPrefabz, transform.position, Quaternion.identity);
}
//dead = true;
DestroyObject(this.gameObject);
}
void Update () {
if(spawnPoint.x - Camera.main.transform.position.x < 11){
closeToCam=true;
}
if (lastHit == "Backboard"){
Invoke ("SelfDestruct", 3);
offScreen= true;
}
else if (movementStopped && !offScreen) {
direction *= -1;
}
else{}
float amtToMove = EnemySpeed * Time.deltaTime * direction;
movement.x = amtToMove;
movement.y -= gravity * Time.deltaTime;
if(closeToCam && !dead){
move (movement);
}
if (this.transform.position.y < -6){
selfDestruct();
}
}
void startFiring(){InvokeRepeating ("spawnBullet", shootSpeed, shootSpeed);}
void spawnBullet(){
Instantiate (bullet, transform.position, Quaternion.identity);
}
public void SetCollider(Vector3 size, Vector3 center){
colliderX.size = size;
colliderX.center= center;
s = size * colliderScaleX;
c = center * colliderScaleX;
}
}