期末unity遊戲開發

1.建立遊戲場景模型dom

使用unity的地形
或者通常都是在建模軟件裏面作好,好比我就是用C4D.導出FBX文件
注意貼圖丟失的問題,記得放在一個文件夾下面,FBX和貼圖文件:像

clipboard.png

2.導入模型到unity編輯器

模型的天空貼圖會丟失,那就建立unity本身的天空材質:[建立unity天空方法][1]
創建cube盒子在模型裏,要否則會穿過盒子,也沒有更好的方法呢?🤔
導入人物(這裏是下載unity-chan)

3.攝像機人物跟隨oop

若是模型裏有攝像機或者c4d編輯器,記得要刪除。
創建scrip文件夾
創建跟隨腳本,放在攝像機裏面,攝像機放在人物裏面:[unity控制攝像機跟隨][2]

4.場景的切換
save 場景,添加到build
寫代碼綁到cube上動畫

5.unity自帶的UI系統
Text 遊戲時間,遊戲人物血條ui

6.怪物AI系統;this

(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()
    {

    }


}
相關文章
相關標籤/搜索