0
votes

I am going to try to explain this in the easiest way. I am making some AI for an enemy player. I want the AI to move backwards if its ray cast hits the player, however I am having a serious issue. [I will explain below code]

Code;

using UnityEngine;
using System.Collections;

public class NewEnemyAI : MonoBehaviour 
{
    public GameObject enemy;
    public Transform target;
    public float movementSpeed = 2f;

    private CharacterController enemyCharacterController;
    private bool enemyHit;
    private Vector3 startPos;
    private Vector3 endPos;
    private float distance = 7f;
    private float lerpTime = 0.3f;
    private float currentLerpTime = 0f;
    private bool avoid;
    private bool updateOn = true;
    private bool hitPlayer = false;


    // Use this for initialization
    void Start () 
    {
        avoid = false;
    }

    // Update is called once per frame
    void Update () 
    {
        //raycasting
        if (Physics.Raycast(transform.position, transform.forward, 2))
        {
            startPos = enemy.transform.position;
            endPos = enemy.transform.position + Vector3.back * distance;
            avoid = true;
        }
        else
        {
            avoid = false;
        }

        if (avoid == true)
        {
            currentLerpTime += Time.deltaTime;
            if (currentLerpTime >= lerpTime)
            {
                currentLerpTime = lerpTime;
            }

            float perc = currentLerpTime/lerpTime;
            enemy.transform.position = Vector3.Lerp(startPos, endPos, perc);

            avoid = false;
        }

        if (avoid == false)
        {
            currentLerpTime = 0;
            transform.LookAt(target);
            transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed);
        }
    }
}

When the enemy's ray cast hits the player, I set a bool to true, which when that is true, it makes the player move back. However because the player is moving back, the ray cast doesn't hit the player anymore and then this whole process starts again and the enemy just rubber bands because it is jumping from code to code. Is there a way for the code to ignore the ray cast if statement and permanently set the avoid bool to true until told otherwise. thanks! [I am relatively new to C# so please don't criticize my code too much].

1

1 Answers

2
votes

Easy fix: add timer to if statement:

if (Physics.Raycast(transform.position, transform.forward, 2) && timePassed(timestamp))

And then you can implement timePassed function basing on this example:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public GameObject projectile;
    public float fireRate = 0.5F;
    private float nextFire = 0.0F;
    void Update() {
        if (Input.GetButton("Fire1") && Time.time > nextFire) {
            nextFire = Time.time + fireRate;
            GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
        }
    }
}

source: https://docs.unity3d.com/ScriptReference/Time-time.html

However, you should consider refactoring your code keeping in mind rules of objected oriented programming - learning from good sources can bring lot at the end of the day, then making mistakes in bed designed code and trying to patch them.

I recommend going through some good C# tutorials from desk to desk, and writing games to practice what have you learned.