zoukankan      html  css  js  c++  java
  • 跟随玩家

    using UnityEngine;
    using System.Collections;
    
    public class EnemyAI : MonoBehaviour 
    {
        public int moveSpeed = 1;//怪物移动速度
        public int rotationSpeed = 5;//怪物转身数独
        
        private Transform target;//目标玩家
        private Transform myTransform;//目标怪物
        private Vector3 targetPosition;//目标玩家的坐标
        private float maxDistance;//玩家跟怪物间的距离
        
        void Awake()
        {
            myTransform = transform;//当前怪物的transform付给myTransform
        }
        
        void Start () 
        {
            GameObject player = GameObject.FindGameObjectWithTag ("Player");//找到tag为player的对象
            target = player.transform;//定义player就是目标玩家
        }
        
        void Update () 
        {
            Debug.DrawLine (target.position, myTransform.position, Color.red);//在玩家跟怪物中间画一条直的红线方便查看
            
            //设置怪物转身,正面始终朝向玩家
            targetPosition = new Vector3 (target.position.x, 0, target.position.z);//得到怪物脚下xz坐标
            myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation(targetPosition - myTransform.position), rotationSpeed * Time.deltaTime);//挂物转身朝向玩家
            
            //设置怪物想玩家移动
            maxDistance = Vector3.Distance(targetPosition, myTransform.position);//获取玩家与怪物之间的距离
            if(maxDistance >= 2)
            {
                //当距离大于两米时移动
                myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;//让怪物朝着自己的正面移动
            }
            else
            {
                //当距离小于两米时的动作
            }
        }
    }
  • 相关阅读:
    python 学习
    快速排序
    U3D AStar 案例
    U3D AStar 算法原理
    .net core 实时通讯
    .net 算法复杂度
    U3D MVC 背包案例
    U3D 对象池
    Unity网络基础(NetWork)
    U3D Socket实现简易通讯
  • 原文地址:https://www.cnblogs.com/123ing/p/3900607.html
Copyright © 2011-2022 走看看