- Home /
This question was
closed Jan 12, 2019 at 04:06 AM by
jsbloo for the following reason:
Other
Question by
jsbloo · Jan 12, 2019 at 03:10 AM ·
navmeshagentnavigationnewbieclick to move
Click to move script, rotates player but does not move player to clicked destination
Hello, I am learning unity.
I followed this video: https://www.youtube.com/watch?v=OYwQFpJIFGg&t=1722s
I have baked my static terrain, I have added a NavigationAgent to my player model. When I click my player rotates to the clicked position but it does not move. I am not sure why.....
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ClickToMove : MonoBehaviour
{
public int clickRange = 100;
//public LayerMask whatCanBeClickedOn;
private NavMeshAgent navMeshAgent;
// private bool moving;
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetButtonDown("Fire1"))
{
if(Physics.Raycast(ray, out hit, clickRange))
{
//moving = true;
navMeshAgent.destination = hit.point;
navMeshAgent.isStopped = false;
}
}
}
}
I guess that the issue stems from my terrain. I am using this free water asset: https://assetstore.unity.com/packages/tools/particles-effects/lowpoly-water-107563
It has a box colider.
Comment