第一次用unity b站轻松塔防游戏

一.跟教程走把地面和障碍做好

至于寻路我对自带的自动寻路有印象,就用的那个组件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;

public class Find : MonoBehaviour
{
    public GameObject Endnote;
    public Transform TraFindDestination ;//寻路目标点
    public UnityEngine.AI.NavMeshAgent _Agent;//寻路组件
    // Start is called before the first frame update

    void Start()
    {
        _Agent = this.GetComponent<UnityEngine.AI.NavMeshAgent>();
        Endnote = GameObject.Find("end");
        TraFindDestination = Endnote.transform;
        //Debug.Log("666");
    }

    // Update is called once per frame
    void Update()
    {
        //Debug.Log("666");set finding
        if (_Agent && TraFindDestination) 
        {
            //Debug.Log("7");
            _Agent.SetDestination(TraFindDestination.transform.position);
        }
        
        Vector3 dir = transform.position - TraFindDestination.position;
        if (dir.x == 0 && dir.z == 0)
        {
            Destroy(Endnote);
            return;
        }
    }
}

 就这一点代码我先是不太会整vector3判断,就用x,z了.后加了个摧毁目标(原来那个摧毁自己简直有问题)

然后就出问题了...

代码没问题,但渲染的时候没给障碍标明Navigation Static,导致敌人直接从上面走过去了.不过开始我以为是障碍不够高(当时墙只有0.1)又是调步高角度,又是调代理大小...

二.然后创建敌人生成器

把敌人一删,我发现生成敌人可以,但它们不动!

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Enemy_Spawner : MonoBehaviour
{
    
    public GameObject enemyPrefrb;//敌人预制体
    public Transform start;
    public float spawnImterval = 6f;//敌人生成时间间隔
    float cutDown;
    void Start()
    {
        cutDown = spawnImterval;
    }

    // Update is called once per frame
    void Update()
    {
        cutDown -= Time.deltaTime;
        if (cutDown <= 0)
        {
            cutDown = spawnImterval;
            SpawnEnemy();
        }
    }

    void SpawnEnemy() 
    {
        Instantiate(enemyPrefrb, start.position, start.rotation);
         
    }

}

当时觉得世界炸了...

这不该生成后自动运行脚本吗?翻视频教程,在csdn上查,找不到相关说明(太基础了),而且我有自动运行的印象,我又在Find里Debug.Log("666");(hhh)卡了一晚上;第二天,先检查了一遍,哦!

加速度定的0.0001...(可能是把创建过程的敌人删掉改的速度没保存)然后动的没生的快...把前面的挤走了...

三.摄像机控制

这我能跟教程一样?(bushi)教程加了个范围边界,我觉得应该保留游戏特色(),没加;不过整了个空格复位;

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

public class Camera_move : MonoBehaviour
{
    public float moveSpeed = 5;
    float scrollSpeed = 600;
    float space = 10;
    Vector3 Origin;
    
    // Start is called before the first frame update
    void Start()
    {
        Origin = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.A) || Input.mousePosition.x < space)
        {
            transform.position += Vector3.left * moveSpeed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.D) || Input.mousePosition.x > Screen.width - space)
        {
            transform.position += Vector3.right * moveSpeed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.W) || Input.mousePosition.y > Screen.height - space)
        {
            transform.position += Vector3.forward * moveSpeed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.S) || Input.mousePosition.y < space)
        {
            transform.position += Vector3.back * moveSpeed * Time.deltaTime;
        }

        float scroll = Input.GetAxis("Mouse ScrollWheel");
        if (scroll != 0) 
        {
            transform.position += Vector3.up * scrollSpeed * scroll * Time.deltaTime;
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            transform.position = Origin;
           
        }
    }
}

实在是整不明白怎么把transform全覆盖了,只用了position(lll¬ω¬)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值