- Home /
How to make an enemy that walks around a platform?
Hi, I have a quick question. In this game that I'm making I have a plan for an enemy that walks over and walks around platforms (like the buzzy beetle from Mario, it can walk sideways and upside down along a platform). I thought that it would be a menial task, but it turned out to be more complicated than I would like for it to be.
In my code I have devised a way for the enemy to rotate 90 additional degrees (in the clockwise direction) and there are raycasts in place to detect walls and the ground. My issue is that the way that I have the enemy detecting corners is unintuitive. Right now, the enemy ends up a little farther from the corner after each turn so it can't do more than one rotation. My questions is how do I go about what I'm looking for from here? I'm thinking right now I set its transform with Mathf or something similar. I was trying to use the physics system to my advantage but I had no luck.
By the way the enemy does not follow gravity atm.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Alpha : EnemyController
{
public LayerMask groundLayer;
public BoxCollider2D groundCheck2;
private Rigidbody2D rb;
private bool rotated;
public Vector2 norm;
public float zed;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
rotated = false;
zed = transform.eulerAngles.z;
}
private bool WallInFront()
{
RaycastHit2D raycastHit = Physics2D.Raycast(transform.position, transform.right, .5f, groundLayer);
if (raycastHit.collider != null)
{
Debug.DrawRay(transform.position, transform.right, Color.green, .5f);
}
else
{
Debug.DrawRay(transform.position, transform.right, Color.red, .5f);
}
return raycastHit.collider != null;
}
private bool Grounded()
{
float extraHeight = .1f;
RaycastHit2D raycastHit = Physics2D.BoxCast(groundCheck2.bounds.center, groundCheck2.bounds.size, 0f, -transform.up, extraHeight, groundLayer);
if (raycastHit.collider != null)
{
norm = raycastHit.normal;
Debug.DrawRay(raycastHit.point, raycastHit.normal, Color.black);
}
return raycastHit.collider != null;
}
// Update is called once per frame
void Update()
{
zed = Mathf.Round(zed / 90) * 90;
}
private void FixedUpdate()
{
if (Grounded() && !WallInFront())
{
if (zed == 0)
{
rb.velocity = new Vector2(speed * Time.deltaTime, rb.velocity.y);
}
else if (zed == 270)
{
rb.velocity = new Vector2(rb.velocity.x, -speed * Time.deltaTime);
}
else if (zed == 180)
{
rb.velocity = new Vector2(-speed * Time.deltaTime, rb.velocity.y);
}
else if (zed == 90)
{
rb.velocity = new Vector2(rb.velocity.x, speed * Time.deltaTime);
}
}
else if (!Grounded() && !WallInFront() && !rotated)
{
RotateDown();
}
else if (Grounded() && WallInFront())
{
RotateUp();
}
}
void RotateDown()
{
rb.velocity = Vector2.zero;
Quaternion newRot = Quaternion.Euler(0, 0, zed - 90);
transform.rotation = Quaternion.Slerp(transform.rotation, newRot, 1f);
}
void RotateUp()
{
rb.velocity = Vector2.zero;
Quaternion newRot = Quaternion.Euler(0, 0, zed + 90);
transform.rotation = Quaternion.Slerp(transform.rotation, newRot, 1f);
}
}
Any help would be greatly appreciated! If my question is unclear also be sure to let me know!
Your answer
Follow this Question
Related Questions
how do I keep each cloned object at a certain point after trigger 0 Answers
Slowly Change One Object's Rotation To Match Another's 2 Answers
How to rotate an object to "sit" on a sphere 1 Answer
Help, please with rotation input 0 Answers
Control over y-axis limit in rotating object by touch 0 Answers