Question by
soleron · Dec 21, 2017 at 05:01 PM ·
c#controllernavigation
[Beginner] How can I improve my click to move script?
I have created a simple click to move script and while it mostly works, it is not reliable. It has 2 issues:
A. It's hit or miss in close distance. i.e. when a small portion of the Navmesh is visible, the character doesn't always move. It works a lot better from a larger distance when more of the Navmesh shows but that distance is not helping my intended gameplay.
B. The character does not move all the way to the clicked location. Especially when I have zoomed close.
Here's my script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ClickMove : MonoBehaviour {
private Animator mAnim;
private NavMeshAgent mNavAgent;
private bool mwalk = false;
// Use this for initialization
void Start ()
{
mAnim = GetComponent<Animator>();
mNavAgent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetMouseButton(0))
{
if(Physics.Raycast(ray, out hit, 800))
{
mNavAgent.destination = hit.point;
}
}
if(mNavAgent.remainingDistance <= mNavAgent.stoppingDistance)
{
mwalk = false;
}
else
{
mwalk = true;
}
mAnim.SetBool("walking", mwalk);
}
}
Comment
Your answer
Follow this Question
Related Questions
Need help with FPScontroller 0 Answers
Mega Man Style Camera Movement 0 Answers
Navigating with controller doesn't stops if there is a UI Toggle. 0 Answers
help to connect two objects together 0 Answers