当前位置:   article > 正文

【unity学习笔记】第一人称射击游戏(2)

【unity学习笔记】第一人称射击游戏(2)

这篇博客主要记录一下敌人自动寻路以及点击地面移动的相关知识点

简单的自动寻路功能,新增了可以去多个位置巡逻

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

public class _nav : MonoBehaviour
{
    private NavMeshAgent foxNav;
    public Transform playerTrans;
    public Transform[] transforms;
    float time = 0;
    int i = 0;
    void Start()
    {
        foxNav = GetComponent<NavMeshAgent>();
        foxNav.SetDestination(transforms[i].position);
    }

    // Update is called once per frame
    void Update()
    {
        //自动寻路
       // foxNav.SetDestination(playerTrans.position);
        //若距离很小,就设为到达
        if (Vector3.Distance(foxNav.nextPosition, foxNav.destination) < 0.2f)
        {
            this.transform.position = foxNav.nextPosition;
            time += Time.deltaTime;
            if (time > 3)
            {
                time = 0;
                foxNav.SetDestination(transforms[(i++)%transforms.Length].position);
            }
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

用鼠标点击地面,游戏对象便会走到相应位置

//摄像机跟随的设置

public class _cameraPos : MonoBehaviour
{
    public Transform Player;
    Vector3 cameraPos;
    void Start()
    {
        //固定三维向量差
        cameraPos = transform.position - Player.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        
        transform.position = Player.transform.position + cameraPos;
        cameraPos = transform.position - Player.transform.position;
        //  transform.position = new Vector3(Player.transform.position.x + cameraPos.x, Player.transform.position.y + cameraPos.y, Player.transform.position.z + cameraPos.z);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

具体代码

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

public class _Game2PlayerMove : MonoBehaviour
{
    private NavMeshAgent playerNav;
    void Start()
    {
        playerNav = GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        //通过鼠标点击人物移动
        if (Input.GetMouseButtonDown(0))
        {
            //从鼠标位置发射一条射线
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            //如果射线检测到物体/碰撞盒
            if (Physics.Raycast(ray, out hit))
            {

                playerNav.SetDestination(hit.point);

                //物体下一步的位置
               // Debug.Log(playerNav.nextPosition + "nextPosition");
                //物体终点的位置
                //Debug.Log(playerNav.destination + "destination");
            }
        }

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/104164
推荐阅读
相关标签
  

闽ICP备14008679号