Commit 2f0a20ac authored by Cody A Burchett's avatar Cody A Burchett

toggle and delete

parent 45f14ace
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseEnemy : MonoBehaviour {
public float speed = 2f;
Rigidbody2D body;
protected SpriteRenderer sr;
public Transform rightCollisionPos;
public Transform leftCollisionPos;
void Start()
{
body = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
}
protected void Move()
{
body.transform.Translate((sr.flipX ? -transform.right:transform.right) * speed * Time.deltaTime);
}
protected bool CheckCollision(Vector2 dir)
{
Vector3 position = sr.flipX ? leftCollisionPos.position : rightCollisionPos.position;
RaycastHit2D hitSide = Physics2D.Raycast(position, sr.flipX ? -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))
{
Move();
}
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