美文网首页
Unity Creating a Click to Move G

Unity Creating a Click to Move G

作者: 86a262e62b0b | 来源:发表于2019-07-23 22:39 被阅读0次

    官方教材:
    https://unity3d.com/cn/learn/tutorials/topics/navigation/creating-click-move-game?playlist=17105

    using UnityEngine;
    using System.Collections;
    
    namespace CompleteProject
    {
    
        public class ClickToMove : MonoBehaviour {
    
            public float shootDistance = 10f;
            public float shootRate = .5f;
            public PlayerShooting shootingScript;
    
            private Animator anim;
            private NavMeshAgent navMeshAgent;
            private Transform targetedEnemy;
            private Ray shootRay;
            private RaycastHit shootHit;
            private bool walking;
            private bool enemyClicked;
            private float nextFire;
    
            // Use this for initialization
            void Awake () 
            {
                anim = GetComponent<Animator> ();
                navMeshAgent = GetComponent<NavMeshAgent> ();
            }
            
            // Update is called once per frame
            void Update () 
            {
                Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                RaycastHit hit;
                if (Input.GetButtonDown ("Fire2")) 
                {
                    if (Physics.Raycast(ray, out hit, 100))
                    {
                        if (hit.collider.CompareTag("Enemy"))
                        {
                            targetedEnemy = hit.transform;
                            enemyClicked = true;
                        }
    
                        else
                        {
                            walking = true;
                            enemyClicked = false;
                            navMeshAgent.destination = hit.point;
                            navMeshAgent.Resume();
                        }
                    }
                }
    
                if (enemyClicked) {
                    MoveAndShoot();
                }
    
                if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance) {
                    if (!navMeshAgent.hasPath || Mathf.Abs (navMeshAgent.velocity.sqrMagnitude) < float.Epsilon)
                    walking = false;
                } else {
                    walking = true;
                }
    
                anim.SetBool ("IsWalking", walking);
            }
    
            private void MoveAndShoot()
            {
                if (targetedEnemy == null)
                    return;
                navMeshAgent.destination = targetedEnemy.position;
                if (navMeshAgent.remainingDistance >= shootDistance) {
                
                    navMeshAgent.Resume();
                    walking = true;
                }
    
                if (navMeshAgent.remainingDistance <= shootDistance) {
                
                    transform.LookAt(targetedEnemy);
                    Vector3 dirToShoot = targetedEnemy.transform.position - transform.position;
                    if (Time.time > nextFire)
                    {
                        nextFire = Time.time + shootRate;
                        shootingScript.Shoot(dirToShoot);
                    }
                    navMeshAgent.Stop();
                    walking = false;
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Unity Creating a Click to Move G

          本文链接:https://www.haomeiwen.com/subject/fhpzlctx.html