I'm making a 2D platformer with Unity. I have an enemy patrol that shoots bullet. The enemy is working fine until it reaches to a point that should be turn around. (when collides with an obstacle or is near the ledges) The problem is that the enemy flips rapidly in this situation. Also it only shoots to the right despite its facing direction. I'm pretty sure that the problem is from either the bullet or the pistol script because the enemy completely works when the pistol game object is disabled. My goal is to make the enemy flip at those points like before while it shoots the bullets in its facing direction.
Enemy script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : PhysicsObject
{
[Header("Attributes")]
private int direction = 1;
[SerializeField] private float maxSpeed;
[SerializeField] private LayerMask rayCastLayerMask; //Which layer do we want the raycast to interact with?
[SerializeField] private Vector2 rayCastOffset; //Offset from the center of the raycast origin
[SerializeField] private float rayCastLength = 2;
bool isFacingLeft;
[Header("References")]
private RaycastHit2D rightLedgeRaycastHit;
private RaycastHit2D leftLedgeRaycastHit;
private RaycastHit2D rightWallRaycastHit;
private RaycastHit2D leftWallRaycastHit;
void Update()
{
targetVelocity = new Vector2(maxSpeed * direction, 0);
if (targetVelocity.x < -.01)
{
transform.localScale = new Vector2(-1, 1);
isFacingLeft = true;
}
else if (targetVelocity.x > .01)
{
transform.localScale = new Vector2(1, 1);
isFacingLeft = false;
}
//Check for right ledge!
rightLedgeRaycastHit = Physics2D.Raycast(new Vector2(transform.position.x + rayCastOffset.x, transform.position.y + rayCastOffset.y), Vector2.down, rayCastLength);
Debug.DrawRay(new Vector2(transform.position.x + rayCastOffset.x, transform.position.y + rayCastOffset.y), Vector2.down * rayCastLength, Color.blue);
if (rightLedgeRaycastHit.collider == null) direction = -1;
//Check for left ledge!
leftLedgeRaycastHit = Physics2D.Raycast(new Vector2(transform.position.x - rayCastOffset.x, transform.position.y + rayCastOffset.y), Vector2.down, rayCastLength);
Debug.DrawRay(new Vector2(transform.position.x - rayCastOffset.x, transform.position.y + rayCastOffset.y), Vector2.down * rayCastLength, Color.green);
if (leftLedgeRaycastHit.collider == null) direction = 1;
//Check for right wall!
rightWallRaycastHit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), Vector2.right, rayCastLength, rayCastLayerMask);
Debug.DrawRay(new Vector2(transform.position.x, transform.position.y), Vector2.right * rayCastLength, Color.red);
if (rightWallRaycastHit.collider != null) direction = -1;
//Check for left wall!
leftWallRaycastHit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), Vector2.left, rayCastLength, rayCastLayerMask);
Debug.DrawRay(new Vector2(transform.position.x, transform.position.y), Vector2.left * rayCastLength, Color.magenta);
if (leftWallRaycastHit.collider != null) direction = 1;
}
}
Pistol script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pistol : MonoBehaviour
{
[SerializeField] GameObject bullet;
[SerializeField] Transform bulletSpawnPos;
private bool IsShooting;
[SerializeField] private float shootDelay = .5f;
private bool isFacingLeft;
void Start()
{
InvokeRepeating("Shoot",4f, shootDelay);
Rigidbody2D rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (IsShooting) return;
IsShooting = true;
GameObject b = Instantiate(bullet);
b.GetComponent<Bullet>().StartShoot(isFacingLeft);
b.transform.position = bulletSpawnPos.transform.position;
Invoke("ResetShoot", shootDelay);
}
void ResetShoot()
{
IsShooting = false;
}
}
Bullet script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
[SerializeField] float speed;
[SerializeField] int damage;
float timeToDestroy = 3;
public void StartShoot(bool isFacingLeft)
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
if (isFacingLeft)
{
rb.velocity = new Vector2(-speed, 0);
}
else
{
rb.velocity = new Vector2(speed, 0);
}
Destroy(gameObject, timeToDestroy);
}
}