So I am just getting started with a click to move top down whatever, and I am fairly new to coding.
I feel I have exhausted most of the resources I have at hand, (google, unity api, youtube) and now with the “8.1” patch I’m horribly behind.
Can someone link me towards a helpful video for click to move? or better yet, personally help me out.
I would like to use the Navigation Mesh feature Unity offers but cannot seem to get the coding correctly.
basically with
Using UnityEngine;
using UnityEngine.AI;
public class ClickToMove : MonoBehaviour {
private NavMeshAgent myAgent; (I can not get NavMeshAgent to be a variable, I am getting no help with it from the tooltip either.
Use Code tags when posting code to make it more readable.
Take a look at this tutorial:
The basic approach with a Click to Move game involves:
Baking a NavMesh.
Putting an NavMeshAgent component on your character object.
Also on your character object you’ll want another script you create yourself that detects mouse clicks, and sends the destination to the NavMeshAgent. Something like this:
public class MyCharacter : MonoBehaviour {
private NavMeshAgent _navMeshAgent;
void Start() {
_navMeshAgent = this.gameObject.GetComponent<NavMeshAgent>();
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
// Left-click occurred.
// Use Camera.main.ScreenPointToRay to find point in the world that was clicked,
// then use _navMeshAgent.SetDestination() to that point.
}
}
}
using UnityEngine.AI
public class MyCharacter : MonoBehaviour {
private NavMeshAgent _navMeshAgent;
void Start ( ) {
if (Input.GetMouseButtonDown(0)) {
}
}
the sixth line of code is what I’m having issues with its not recognizing “NavMeshAgent”
in the unity API and other tutorials it is telling me to use UnityEngine.AI and with that it is still not working…
You just have syntax errors. If that’s your actual code, you’re just missing a “;” after UnityEngine.AI. You code is also missing a curly brace at the end.