Skip to content
Permalink
aee962d2c7
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
157 lines (125 sloc) 3.44 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;
[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;
}
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"){
if (hasPowerup){
Instantiate (PowerupPrefabz, transform.position, Quaternion.identity);
}
DestroyObject (this.gameObject);
}
}
void Update () {
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;
move (movement);
if (this.transform.position.y < -6){
SelfDestruct();
}
}
void SelfDestruct(){
DestroyObject (this.gameObject);
}
}