Commit 50916e2f authored by Luke A Smith's avatar Luke A Smith

Merge branch 'enemyHorizontal' into 'master'

enemy fixes

See merge request !28
parents 1e755e8d 08b6797d
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseEnemy : MonoBehaviour {
public float speed = 2f;
Rigidbody2D body;
protected SpriteRenderer sr;
protected float startMoveTime;
public Transform rightCollisionPos;
public Transform leftCollisionPos;
void Start()
{
body = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
startMoveTime = Time.time + .4f;
}
protected void Move(Vector2 dir, bool isFlipped)
{
body.transform.Translate((isFlipped ? -dir:dir) * speed * Time.deltaTime);
}
protected bool CheckCollision(Vector2 dir, bool isFlipped)
{
Vector3 position = isFlipped ? leftCollisionPos.position : rightCollisionPos.position;
Debug.DrawRay(position, (isFlipped ? -dir : dir) * .1f, Color.blue, .1f);
Debug.DrawRay(position, -transform.up * .1f, Color.red, .1f);
RaycastHit2D hitSide = Physics2D.Raycast(position, isFlipped ? -dir:dir, .1f);
RaycastHit2D hitDown = Physics2D.Raycast(position, -transform.up, .1f);
if (hitDown.collider != null && hitSide.collider == null)
{
return true;
}
return false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HorizontalEnemy : BaseEnemy {
// Update is called once per frame
void Update () {
if (CheckCollision(transform.right, sr.flipX))
{
if (startMoveTime < Time.time)
{
Move(transform.right, sr.flipX);
}
}
else
{
sr.flipX = !sr.flipX;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VerticalEnemy : BaseEnemy {
public bool leftWall;
void Update()
{
if (CheckCollision(transform.right, leftWall ? !sr.flipX : sr.flipX))
{
if (startMoveTime < Time.time)
{
Move(-transform.up, sr.flipX);
}
}
else
{
sr.flipX = !sr.flipX;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment