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?
SpaceKitten/Assets/Scripts/EnemyPhysics.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
148 lines (115 sloc)
3.21 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; | |
[RequireComponent(typeof(BoxCollider))] | |
public class EnemyPhysics : MonoBehaviour { | |
public LayerMask collisionMask; | |
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; | |
[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);} | |
} | |
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); | |
} | |
} |