简体   繁体   中英

How can i make the enemy to move to next waypoint if there ius a very big waypoint in the middle?

If the waypoints are small size for example cubes at size 0.1 or 1 it's fine. When i change the cubes size to 20-30 if there is a situation that there are two waypoints on the way but the enemy should get to the second waypoint he will stuck on the wall of the first waypoint will shake/stutter and will try to go to one of the sides and then in the end he will pass this waypoint and continue to the waypoint he should get the target.

It happen only when the waypoints(cubes) are very big and if a waypoint block the waypoint the enemy should get.

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;

public class WayPoints : MonoBehaviour {

    public GameObject[] waypoints;
    public Transform target;
    public float moveSpeed = 10f;
    public float moveSpeed1 = 10f;
    public float slowDownSpeed = 3f;
    public float reverseSlowDownSpeed = 3f;
    public float rotationSpeed = 1f;
    private Transform myTransform;
    private int targetsIndex = 0;
    private Vector3 originalPosition;
    private GameObject[] robots;

    public Transform reverseTarget;
    private int reverseTargetsIndex = 0;
    private Vector3 reverseOriginalPosition;

    public bool random = false;

    void Awake()
    {
        myTransform = transform;
    }
    // Use this for initialization
    void Start()
    {
        waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
        robots = GameObject.FindGameObjectsWithTag("Robots");

        AddColliderToWaypoints();
        originalPosition = robots[0].transform.position;
        reverseOriginalPosition = robots[1].transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (MyCommands.walkbetweenwaypoints == true)
        {
            WayPointsAI();
            ReverseWayPointsAI();
        }

        DrawLinesInScene();
    }

    private void WayPointsAI()
    {
        if (targetsIndex == waypoints.Length)
            targetsIndex = 0;
        target = waypoints[targetsIndex].transform;
        float distance = Vector3.Distance(robots[0].transform.position, target.transform.position);
        robots[0].transform.rotation = Quaternion.Slerp(robots[0].transform.rotation, Quaternion.LookRotation(target.position - robots[0].transform.position), rotationSpeed * Time.deltaTime);

        //move towards the player
        if (distance < 30)
        {
            robots[0].transform.position += robots[0].transform.forward * slowDownSpeed * Time.deltaTime;
        }
        else
        {
            robots[0].transform.position += robots[0].transform.forward * moveSpeed * Time.deltaTime;
        }
        if (distance < target.transform.localScale.magnitude)
        {
            targetsIndex++;
        }
    }

    private void ReverseWayPointsAI()
    {
        if (reverseTargetsIndex == 0)
            reverseTargetsIndex = waypoints.Length -1;
        reverseTarget = waypoints[reverseTargetsIndex].transform;
        float distance = Vector3.Distance(robots[1].transform.position, reverseTarget.transform.position);
        robots[1].transform.rotation = Quaternion.Slerp(robots[1].transform.rotation, Quaternion.LookRotation(reverseTarget.position - robots[1].transform.position), rotationSpeed * Time.deltaTime);

        //move towards the player
        if (distance < 30)
        {
            robots[1].transform.position += robots[1].transform.forward * reverseSlowDownSpeed * Time.deltaTime;
        }
        else
        {
            robots[1].transform.position += robots[1].transform.forward * moveSpeed1 * Time.deltaTime;
        }
        if (distance < reverseTarget.transform.localScale.magnitude)
        {
            reverseTargetsIndex--;
        }
    }

    void RandomWayPointsAI()
    {
        if (random == true)
        {
            int index = Random.Range(0, waypoints.Length);
            target = waypoints[index].transform;
        }
    }

    void DrawLinesInScene()
    {
        // draw lines between each checkpoint //
        for (int i = 0; i < waypoints.Length - 1; i++)
        {
            Debug.DrawLine(waypoints[i].transform.position, waypoints[i + 1].transform.position, Color.blue);
        }

        // draw a line between the original transform start position 
        // and the current transform position //
        Debug.DrawLine(originalPosition, robots[0].transform.position, Color.red);
        Debug.DrawLine(reverseOriginalPosition, robots[1].transform.position, Color.red);

        // draw a line between current transform position and the next waypoint target
        // each time reached a waypoint.
        if (target != null)
            Debug.DrawLine(target.transform.position, robots[0].transform.position, Color.green);
        if (reverseTarget != null)
            Debug.DrawLine(reverseTarget.transform.position, robots[1].transform.position, Color.green);
    }

    void AddColliderToWaypoints()
    {
        foreach (GameObject go in waypoints)
        {
            SphereCollider sc = go.AddComponent<SphereCollider>() as SphereCollider;
            sc.isTrigger = true;
        }
    }
}

I tried to make

if (distance < reverseTarget.transform.localScale.magnitude)

I tried before it to make

if (distance < reverseTarget.tranform.localscale.x)

Or

if (distance < reverseTarget.tranform.localscale.x / 2)

Same with target and the first AI function. But nothing is working. Maybe i should find the waypoints radius ? Not sure how to solve it.

What OP seems to have wanted was a way to give the AI pathfinding so that when the AI wanted to go from (dynamic/code generated) point A to B and there was an obstacle between, the AI wouldn't get stuck in the obstacle.

Unity offers NavMesh and NavMesh Obstacle , a simple pathfinding tool Unity gives us to achieve a smart AI with little effort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM