Problem with the Enemy movement:
when I start play and try to move my player my enemy stops walking. Same script worked before but i don’t know what happened this time
Send help pls
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : PhysicsObject
{
[Header("Attributes")]
[SerializeField] private int attackPower = 10;
private int direction = 1;
public int health = 100;
private int maxHealth = 100;
[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;
[Header("References")]
private RaycastHit2D rightLedgeRaycastHit;
private RaycastHit2D leftLedgeRaycastHit;
private RaycastHit2D rightWallRaycastHit;
private RaycastHit2D leftWallRaycastHit;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public void Update()
{
targetVelocity = new Vector2(maxSpeed * direction, 0);
//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 = -2;
//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 = -2;
//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;
//If health < 0, destroy me
if (health <= 0)
{
Destroy(gameObject);
}
}
//If I collide with the player, hurt the player (health is going to decrease, update the UI)
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject == NewPlayer.Instance.gameObject)
{
//Hurt the player, then update the UI!
NewPlayer.Instance.health -= attackPower;
NewPlayer.Instance.UpdateUI();
}
}
}