D
D
Dmitry Lolikov2017-06-10 07:19:03
2D
Dmitry Lolikov, 2017-06-10 07:19:03

How to make the given visibility for the bot?

There is a bot. He follows me, kills, etc., but he follows me because the character has points that he (the bot) follows.
I need the bot to follow me at a certain distance so that when I leave this zone, it does not follow me.
Here is the code:

using UnityEngine;
using System.Collections;

public class Enemies : MonoBehaviour
{

    public Transform[] patrolpoints;
    int currentPoint;
    public float speed = 0.5f;
    public float timestill = 2f;
    public float sight = 3f;
    Animator anim;
    public float force;

    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
        StartCoroutine("Patrol");
        anim.SetBool("walking", true);
        Physics2D.queriesStartInColliders = false;
    }

    // Update is called once per frame
    void Update()
    {

        RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.localScale.x * Vector2.right, sight);
        if (hit.collider != null && hit.collider.tag == "Player")
            GetComponent<Rigidbody2D>().AddForce(Vector3.up * force + (hit.collider.transform.position - transform.position) * force);
    }


    IEnumerator Patrol()
    {
        while (true)
        {

            if (transform.position.x == patrolpoints[currentPoint].position.x)
            {
                currentPoint++;
                anim.SetBool("walking", false);
                yield return new WaitForSeconds(timestill);
                anim.SetBool("walking", true);
            }


            if (currentPoint >= patrolpoints.Length)
            {
                currentPoint = 0;
            }

            transform.position = Vector2.MoveTowards(transform.position, new Vector2(patrolpoints[currentPoint].position.x, transform.position.y), speed);

            if (transform.position.x > patrolpoints[currentPoint].position.x)
                transform.localScale = new Vector3(-1, 1, 1);
            else if (transform.position.x < patrolpoints[currentPoint].position.x)
                transform.localScale = Vector3.one;


            yield return null;


        }
    }


    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Projectile")
            Destroy(this.gameObject, 0.1f);
    }


    void OnDrawGizmos()
    {
        Gizmos.color = Color.red;

        Gizmos.DrawLine(transform.position, transform.position + transform.localScale.x * Vector3.right * sight);

    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheTalion, 2017-06-10
@Lololishka

It is not visible here that the character follows someone - he walks through the dots.
If you need to make the bot move behind you only in a certain zone, then you can limit the zone with a trigger that would react to the character’s entry and exit and inform the patrolling about it.
If you need the character to lag behind at some distance, then just take the distance from the character to the patrolman and check whether it is observed (in the search you can find how to get the distance as a number from
two vectors).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question