Trying to make a "bus" for a game

I’m trying to make a bus for a game that will tavel A-B and then back and repeat. I have objects and scripts for the start and end of the route and I have baked NavMesh. It goes towards the RouteEnd but then it stops there and doesn’t go back. All the objects has colliders and has a tag. I also double checked tag names.I also see now that collissions doesn’t even work :confused: Here is my code;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

public class BusMovement : MonoBehaviour

{

[SerializeField] Transform target;

NavMeshAgent navMeshAgent;

[SerializeField] Transform target2;

[SerializeField] int targeted;



// Start is called before the first frame update

void Start()

{

    navMeshAgent = GetComponent<NavMeshAgent>();

    target = FindObjectOfType<RoadEnd>().transform;

    targeted = 2;

    target2 = FindObjectOfType<RoadStart>().transform;

}

// Update is called once per frame

void Update()

{

    if (targeted == 2)

    {

        navMeshAgent.SetDestination(target.position);

    }

    else

    {

        navMeshAgent.SetDestination(target2.position);

    }

}

void OnCollisionEnter(Collision other)

{

if (other.gameObject.tag == "RestartBusRoute")

{

    Debug.Log("Restarting");

    targeted = 1;

    target.gameObject.SetActive(false);

    navMeshAgent = GetComponent<NavMeshAgent>();

    target2 = FindObjectOfType<RoadStart>().transform;

}

else if (other.gameObject.tag == "StartBusRoute")

{

   

    targeted = 2;

    target2.gameObject.SetActive(false);

    navMeshAgent.GetComponent<NavMeshAgent>();

    target = FindObjectOfType<RoadEnd>().transform;

   

}



}

}

Maybe try looking into using Timeline or a Pathfinding algorithm, those are both things to make something go from a-b and b-a. My knowledge of NavMesh is limited, but I think it would be pretty hard to do what you are trying to do with it.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms