Unity5 Survival Shooter开发笔记2

版权声明:所有的博客仅仅作为个人笔记使用!!!!!!! https://blog.csdn.net/qq_35976351/article/details/83114221

相机跟随人物移动

先介绍一个属性:

public static float deltaTime;

这是一个只读的属性,返回上一帧到这一帧的时间。如果你想要在每一帧增加或者减少数据,需要用数据乘以deltaTime这个数据,比如:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        // 沿着z轴方向,每一帧移动10米
        float translation = Time.deltaTime * 10;
        transform.Translate(0, 0, translation);
    }
}

游戏开发中,相机的移动处理非常重要,在这里给出通用的方法。

public class CameraFollow : MonoBehaviour {

    public Transform target;      // 相机跟踪的目标
    public float smoothing = 5f;  // 平滑移动的处理

    Vector3 offset;   // 保留目标和相机的初始化偏移位置

    private void Start() {
    	// 计算偏移量
        offset = transform.position - target.position;
    }

    private void FixedUpdate() {
    	// 每一次移动都要加上偏移位置
        Vector3 targetCamPos = target.position + offset;
        // 平滑处理,需要添加时间,注意是每一帧的
        transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
    }
}

UnityEngine.AIModule部分的导航模块。

导航和寻找路径简介

导航系统可以让问我们创建能在世界中自主寻路的角色,它可以根据具体的场景进行创建。动态的障碍可以在运行时状态改变对象的移动路径

具体参考:https://docs.unity3d.com/Manual/Navigation.html
创建NavMesh的方法:https://docs.unity3d.com/Manual/nav-BuildingNavMesh.html

创建完Navigation组件后,需要单独进行处理。具体参考手册。

NavMesh模块

该模块是个类,用于寻找路径,并调整对象的路径和规避方式。可以用于二维平面或者空间。使用该类的前提是必须先把Scene进行bake处理。

详细的参考:https://docs.unity3d.com/ScriptReference/AI.NavMesh.html

NavMeshAgent模块

指定场景中的一个可以移动的对象,该对象可以进行导航有关的操作。

具体参考:https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent.html

本例中的追踪方式

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour
{
    Transform player;
    UnityEngine.AI.NavMeshAgent nav;


    void Awake ()
    {
        player = GameObject.FindGameObjectWithTag ("Player").transform;
        nav = GetComponent <UnityEngine.AI.NavMeshAgent> ();
    }


    void Update () {
        // 设定追踪的目标
    	nav.SetDestination (player.position);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35976351/article/details/83114221