Skip to content
Permalink
6d0fa95a4b
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
135 lines (107 sloc) 3.14 KB
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider))]
public class CatScript : MonoBehaviour {
public LayerMask collisionMask;
private BoxCollider colliderX;
private Vector3 s;
private Vector3 c;
public float EnemySpeed;
public float gravity;
private float skin = .005f;
private float dirX;
private float dirY;
private float direction = 1;
private Vector3 movement;
private Vector3 originalSize;
private Vector3 originalCenter;
private float colliderScaleX;
[HideInInspector]
public bool grounded;
[HideInInspector]
public bool movementStopped;
Ray rayV;
Ray rayD;
Ray rayH;
RaycastHit hit;
void Start(){
//audio.Play ();
colliderX = GetComponent<BoxCollider>();
s = colliderX.size;
c = colliderX.center;
colliderScaleX = transform.localScale.x;
SetCollider (s, c);
}
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);
transform.Translate(finalTransform, Space.World);
//movement = new Vector3(finalTransform.x, finalTransform.y, 0);
}
void Update () {
move (movement);
if (transform.position.x > 11){SelfDestruct ();}
float amtToMove = EnemySpeed * Time.deltaTime * direction;
movement.x = amtToMove;
movement.y -= gravity * Time.deltaTime;
}
void SelfDestruct(){
DestroyObject (this.gameObject);
}
public void SetCollider(Vector3 size, Vector3 center){
colliderX.size = size;
colliderX.center= center;
s = size * colliderScaleX;
c = center * colliderScaleX;
}
}