- Home /
when i collide with npc he keeps walking.
so i have a waypoint script on npc's. i also have some kind of dialogue what pops when i hit "e". when i do this on a moving npc he keeps walking. i tried certain things but can't get him to stop....
the collider itself is working, because i do get my camerascript to stop on collision and the text "press E to interract" appears.
any advice would be awesome!!
here is my waypoint script :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WaypointSystem : MonoBehaviour {
public GameObject[] Waypoints;
public int num = 0;
public float minDist;
public float speed;
public bool rand = false;
public bool go = true;
public Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
float dist = Vector3.Distance(gameObject.transform.position, Waypoints[num].transform.position);
if (go)
{
if (dist > minDist)
{
Move();
}
else
{
if (!rand) {
if (num + 1 == Waypoints.Length)
{
num = 0;
}
else
{
num++;
}
} else
{
num = Random.Range(0, Waypoints.Length);
}
}
}
}
public void Move()
{
anim.SetFloat("speed", speed);
gameObject.transform.LookAt(Waypoints[num].transform.position);
gameObject.transform.position += gameObject.transform.forward * speed * Time.deltaTime;
}
}
i already have a script where i disable certain things, but not the movenement :
using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI;
public class DialogueAppear : MonoBehaviour {
//[SerializeField] private Canvas customCanvas;
public GameObject cam;
public Text text;
public GameObject walk;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("NPC"))
{
// customCanvas.enabled = true;
cam.GetComponent<CameraControl>().enabled = false;
text.enabled = true;
walk.GetComponent<WaypointSystem>().enabled = false;
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("NPC"))
{
// customCanvas.enabled = false;
cam.GetComponent<CameraControl>().enabled = true;
text.enabled = false;
walk.GetComponent<WaypointSystem>().enabled = true;
}
}
}
New programmer here! any advice would be much appreciated :D sorry for the long read
Questions: WaypointSystem is on the NPC? What gameObject is walk? Is DialogeAppear on the NPC? It's important to know what these scripts are on.
first . let us know where is collider component ? is it on just NPC or ur character ?
it should be on both, make one of them is trigger and to approach in a fashion way to detect with gameobject collided is to use tag system which u r already doing. now go for the print statements
Answer by jimmycrazyskills · Dec 21, 2018 at 06:28 PM
Hi mate, you could potentially modify your WaypointSystem script to have a canMove
variable, then check that canMove
is true in your Update function before you actually call the Move
function.
You could make this variable public so it's accessible from other scripts (such as your dialog appear script so when a dialog is open canMove
is set to false)
This variable could also be set from your collision enter and exit functions to stop the NPC if you walk into it.
Hope this helped =D