nwy140
1/26/2019 - 3:56 AM

RTSMovement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class RTSMovement : MonoBehaviour
{
    private Ray ray;
    private RaycastHit hit;
    private NavMeshAgent navMeshAgent;
    private Rigidbody myRB;

    // object that is right clicked by player // can be an enemy or an objects in world
    private GameObject targetObject;
    public GameObject selectedCircle;
    // current object is selected or not 
    public bool unitSelected;
    // tamed characters will have allowControl set to true
    public bool allowControl = true; 
    //Allow NPC to randomly move and roam around the map.
    public bool allowRandomMove;
    //Walking Radius limit for randomMove
    public float walkRadiusRandomMove = 500f;
    public float moveSpeed = 20f;
    public float distanceToMousePointLimit = 10f;

    void Start(){
        navMeshAgent = GetComponent<NavMeshAgent>();
        myRB = GetComponent<Rigidbody>();
        navMeshAgent.speed = moveSpeed;

        // Don't move at start at all
        ResetDestination();
    }

    // Update is called once per frame
    void Update()
    {
        myRB.velocity = Vector3.zero;
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if(Input.GetMouseButtonDown(1)){
            if(unitSelected){
                SetDestination();
            }
        }
        CheckDestination();

        if(allowControl){
            selectedCircle.SetActive(unitSelected);
        }
    }

    public void SetDestination() {
            if(Physics.Raycast(ray, out hit)){
                targetObject = hit.collider.gameObject;
                // Stop movement and then set new destination
                ResetDestination();
                // if too close to destination, stop going to destination
                if(Vector3.Distance(transform.position, hit.point) < distanceToMousePointLimit){
                    ResetDestination();
                } 
                if(allowControl){
                    // Move character
                    navMeshAgent.destination = hit.point;
                } 
            }   
    }

    public void CheckDestination(){
    // Check if we've reached the destination
        if (!navMeshAgent.pathPending)
        {
            if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
            {
                if (!navMeshAgent.hasPath || navMeshAgent.velocity.sqrMagnitude == 5f)
                {
                    //allow random Movement if not tamed and is not currently walking to a destination
                    if(allowRandomMove && allowControl == false){
                        navMeshAgent.destination = RandomNavPoint(walkRadiusRandomMove);
                    }
                }
            }
        }
    }

    public Vector3 RandomNavPoint (float walkRadius) {
            Vector3 randomDirection = Random.insideUnitSphere * walkRadius;
            randomDirection += transform.position;
            NavMeshHit hit;
            NavMesh.SamplePosition(randomDirection, out hit, walkRadius, 1);
            
            return hit.position;
    }
    public void ResetDestination(){
            // reset destination
            navMeshAgent.Stop();
            navMeshAgent.ResetPath();
    }

    
    public void SetDestination(Vector3 location)
    {
        ResetDestination();
        navMeshAgent.destination = location; 
    }

    public void SetSpeed(int speed)
    {
        moveSpeed = speed;
        navMeshAgent.speed = moveSpeed;

    }
}