1.创建游戏场景模型

使用unity的地形
或者一般都是在建模软件里面做好,比如我就是用C4D.导出FBX文件
注意贴图丢失的问题,记得放在一个文件夹下面,FBX和贴图文件:像

clipboard.png

2.导入模型到unity

模型的天空贴图会丢失,那就创建unity自己的天空材质:[创建unity天空方法][1]
建立cube盒子在模型里,要不然会穿过盒子,也没有更好的方法呢??
导入人物(这里是下载unity-chan)

3.摄像机人物跟随

如果模型里有摄像机或者c4d编辑器,记得要删除。
建立scrip文件夹
建立跟随脚本,放在摄像机里面,摄像机放在人物里面:[unity控制摄像机跟随][2]

4.场景的切换
save 场景,添加到build
写代码绑到cube上

5.unity自带的UI系统
Text 游戏时间,游戏人物血条

6.怪物AI系统;

(1)导入怪物,带动话的
(2)添加角色控制机器 character controller
 (3)传统的动画系统 添加 Animation 设置好size;
(4)写代码,public target 添加,把玩家放入target里面;


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

public class AIControllor : MonoBehaviour
{

    public Transform target;
    public float rotateSpeed = 30.0f;
    public CharacterController characterController;
    private Animation ani;
    public float directionTravektime = 2.0f;//转方向的间隔时间
    private float timeToNewDirection = 0.0f;//计时器

    public float idleTime = 1.5f;

    public float walkeSpeed = 3.0f;
    public float attackMovieSpeed = 5.0f;

    public float attacktDistance = 15.0f;
    public bool isAttack = false;
    public Vector3 attackPosition = new Vector3(0, 1, 0);
    public float attackRadius = 5.0f;

    public float damage = 1.0f;

    public float lastAttackTime = 0.0f;

    private Vector3 distanceToPlayer;
    // Use this for initialization
    void Start()
    {
        characterController = GetComponent<CharacterController>();
        ani = GetComponent<Animation>();
        if (!target)
        {
            target = GameObject.FindWithTag("Actor").transform;
        }
        //使用动画循环模式。
        ani.wrapMode = WrapMode.Loop;
        ani["EBunny_Death"].wrapMode = WrapMode.Once;
        ani["EBunny_Death"].layer = 5;
        ani["EBunny_Hit"].layer = 3;
        ani["EBunny_Attack"].layer = 1;

        StartCoroutine(InitEnemy());

    }

    IEnumerator InitEnemy()
    {
        while (true)
        {
            yield return StartCoroutine(Idle());
            yield return StartCoroutine(Attack());
        }
    }

    IEnumerator Idle()
    {
        while (true)
        {
            if (Time.time > timeToNewDirection)
            { //到了转方向的时间
                //
                yield return new WaitForSeconds(idleTime);//

                if (Random.value > 0.5)
                {
                    transform.Rotate(new Vector3(0, 5, 0), rotateSpeed);
                }
                else
                {
                    transform.Rotate(new Vector3(0, -5, 0), rotateSpeed);
                }
                timeToNewDirection = Time.time + directionTravektime;

            }
            //zhaun huan zuo biao
            Vector3 walkForward = transform.TransformDirection(Vector3.forward);
            characterController.SimpleMove(walkForward * walkeSpeed);

            //he player de jv li
            distanceToPlayer = transform.position - target.position;
            if (distanceToPlayer.magnitude < attacktDistance)
            { //ru guo jv li xiaoyv xun luo 
                yield break;
            }
            yield return null;
        }

    }

    IEnumerator Attack()
    {
        isAttack = true;
        ani.Play("EBunny_Attack");
        //kan zhe zhu jiao
        transform.LookAt(target);

        Vector3 direction = transform.TransformDirection(Vector3.forward * attackMovieSpeed);
        characterController.SimpleMove(direction);

        bool lostSight = false;
        while (!lostSight)
        {
            //ji shuan player de jv li
            Vector3 location = transform.TransformPoint(attackPosition) - target.position;
            if (Time.time > lastAttackTime + 2.0f && location.magnitude < attackRadius)
            {
                target.SendMessage("ApplyDamage", damage);
                lastAttackTime = Time.time;
            }
            if (location.magnitude > attackRadius)
            {
                lostSight = true;
                yield break;
            }
            yield return null;
        }
        isAttack = false;

        yield return null;

    }

    // Update is called once per frame
    void Update()
    {

    }


}

FengNianya
137 声望17 粉丝

数字媒体技术本科


引用和评论

0 条评论