一.跟教程走把地面和障碍做好
至于寻路我对自带的自动寻路有印象,就用的那个组件
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¬ω¬)