Hi all,
I’ve been working on this for way too long now and realise now my code is very messy having tried so many things in order to try and make this work. All I’ve been trying to do is make my enemy patrol on the platform, which it does fine, then when in range of the player to turn around and increase velocity towards the player but getting the enemy to actually turn around without the flip function being called constantly has been really hard. I had used a ray cast in the opposite direction to the way the enemy was facing but this didn’t seem to work and instead tried to just compare x positions. The difficulty is stopping the enemy from flipping when it is going for the player (having just flipped to change direction towards the player) while still being able to detect if the player has jumped over (as the enemy will need to flip again to pursue) . Here is all the code…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour {
public float velocity;
public float attackVelocity;
public GameObject EndOfPlatform;
bool facingRight = true;
bool inAttackRange = false;
public int maxRange;
public GameObject player;
Vector3 playerPos;
public bool correctDirection = true;
public float preventMultipleCalls;
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
// Use this for initialization
void Start () {
playerPos = player.transform.position;
preventMultipleCalls = 2;
facingRight = true;
}
// Update is called once per frame
void Update (){
if (Vector3.Distance (transform.position, player.transform.position) > maxRange)
inAttackRange = false;
if (Vector3.Distance (transform.position, player.transform.position) <= maxRange)
inAttackRange = true;
if (inAttackRange == false) {
if (facingRight == true){
GetComponent<Rigidbody2D> ().velocity = new Vector2 (velocity, GetComponent<Rigidbody2D> ().velocity.y);
} if (facingRight != true){
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-velocity, GetComponent<Rigidbody2D> ().velocity.y);
}
}
else if (inAttackRange == true) {
if (preventMultipleCalls == 2) {
if (facingRight == true) {
//correctDirection = Physics.Raycast (transform.position, transform.TransformDirection (Vector3.left), 12f);
if (transform.position.x < player.transform.position.x)
correctDirection = false;
}
if (facingRight == false){
if (transform.position.x > player.transform.position.x)
correctDirection = false;
}
//correctDirection = Physics.Raycast (transform.position, transform.TransformDirection (Vector3.right), 12f);
if (correctDirection == false) {
Flip ();
}
if (transform.position.x > player.transform.position.x)
preventMultipleCalls = 3;
if (transform.position.x < player.transform.position.x)
preventMultipleCalls = 1;
}
if (transform.position.x > player.transform.position.x)
preventMultipleCalls = 3;
if (transform.position.x < player.transform.position.x)
preventMultipleCalls = 1;
if (facingRight == true) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (attackVelocity, GetComponent<Rigidbody2D> ().velocity.y);
} if (facingRight != true) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-attackVelocity, GetComponent<Rigidbody2D> ().velocity.y);
}
if (transform.position.x > player.transform.position.x && preventMultipleCalls == 3)
preventMultipleCalls = 2;
if (transform.position.x < player.transform.position.x && preventMultipleCalls == 1)
preventMultipleCalls = 2;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "EndOfPlatform")
{
Flip ();
}
}
}
I have pasted the entire enemy script in here just so you can understand what all the functions and variables all do.
Thanks in advance to anyone who helps me out <3