Question by
kimbangud213 · Mar 14, 2020 at 12:59 PM ·
raycastingwaypoints
move to a list of waypoints with raycast
so i have this player that is selected, and i would like to move it to a loop of waypoints in order or path, and pause to a current waypoint's transform or collider that i clicked else continue as i clicked on another waypoints transform ?any ideas guys..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class WaypointsClick : MonoBehaviour
{
NavMeshAgent nm;
public Transform[] Waypoints;
public int cur_loc = 0;
public float stop_distance;
public bool go = false;
void Start()
{
nm = GetComponent<NavMeshAgent>();
nm.stoppingDistance = stop_distance;
}
void Update()
{
if (Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.transform != null)
{
Collider myBox;
if (myBox = hit.collider.transform.GetComponent<BoxCollider>())
{
go = true;
}
}
}
Debug.DrawLine(transform.position, hit.transform.position);
}
if (go)
{
float dist = Vector3.Distance(transform.position, Waypoints[cur_loc].position);
transform.LookAt(Waypoints[cur_loc].transform);
if (dist < stop_distance)
{
cur_loc += 1;
}
if (cur_loc == Waypoints.Length)
{
cur_loc = 0;
}
if (cur_loc == 0)
{
nm.SetDestination(Waypoints[0].position);
}
if (cur_loc == 1)
{
nm.SetDestination(Waypoints[1].position);
}
if (cur_loc == 2)
{
nm.SetDestination(Waypoints[2].position);
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612224933im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast detect in object (problem) 0 Answers
Object referance not set to an instance of an object? 1 Answer
My RayCast isn't facing forward 1 Answer
ui Buttons rendered on camera 0 Answers