3D遊戲編程——模型與動畫

動畫部分

首先是玩家的動畫控制器:
在這裏插入圖片描述
巡邏兵的動畫控制器:
在這裏插入圖片描述

運行時場景

在這裏插入圖片描述

預製

在這裏插入圖片描述
patrol的設置:
在這裏插入圖片描述
player的設置:
在這裏插入圖片描述
Plane的設置:
在這裏插入圖片描述
Plane的子對象也做了不少設置,這裏不再詳細給出。

Trigger的設置:
在這裏插入圖片描述

代碼部分

在預製部分,我們看到Trigger和patrol裏面都掛載了ColiAction這個腳本,來實現玩家與巡邏兵碰撞時的通知:

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

public class ColiAction : MonoBehaviour
{
    FirstSceneController sceneController;
    GameObject myobject = null;
    GameObject player;
    public delegate void AddScore();
    public static event AddScore myAddScore;

    public delegate void GameOver();
    public static event GameOver myGameOver;

    private void Start()
    {
        sceneController = SSDirector.getInstance().currentSceneController as FirstSceneController;
        player = sceneController.player;
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == player)
        {
            int k = this.name[this.name.Length - 1] - '0';
            myobject = sceneController.patrols[k];
            foreach (var i in sceneController.actionManager.seq)
            {
                if (i.gameObject == myobject)
                {
                    i.enable = false;
                    Vector3 a = new Vector3(myobject.transform.position.x, 0, myobject.transform.position.z);
                    Vector3 b = new Vector3(player.transform.position.x, 0, player.transform.position.z);
                    Quaternion rotation = Quaternion.LookRotation(b - a, Vector3.up);
                    myobject.transform.rotation = rotation;
                }
            }
        }
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject == player)
        {
            int k = this.name[this.name.Length - 1] - '0';
            foreach (var i in sceneController.actionManager.seq)
            {
                if (i.gameObject == myobject)
                {
                    i.enable = true;
                    Vector3 a = new Vector3(myobject.transform.position.x, 0, myobject.transform.position.z);
                    Vector3 b = new Vector3(i.target.x, 0, i.target.z);
                    Quaternion rotation = Quaternion.LookRotation(b - a, Vector3.up);
                    myobject.transform.rotation = rotation;
                }
            }
            myobject = null;
            myAddScore();
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (this.tag == "patrol" && collision.gameObject == player)
        {
            myGameOver();
            var k = collision.gameObject.GetComponent<Animator>();
            k.SetBool("death", true);
        }
    }
    private void Update()
    {
        if (myobject != null && sceneController.flag == 0)
        {
            myobject.transform.position = Vector3.MoveTowards(myobject.transform.position, player.transform.position, 3 * Time.deltaTime);
        }
    }
}

在一個空對象掛載FirstSceneController腳本,來加載場景:

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

public class FirstSceneController : MonoBehaviour, IUserAction, ISceneController
{
    public CCActionManager actionManager;
    public GameObject player;
    public List<GameObject> patrols = new List<GameObject>();
    public PatrolFactory pf;
    public int flag = 0;
    public int score = 0;

    private void Awake()
    {
        SSDirector director = SSDirector.getInstance();
        director.setFPS(60);
        director.currentSceneController = this;
        this.gameObject.AddComponent<PatrolFactory>();
        pf = Singleton<PatrolFactory>.Instance;
        this.gameObject.AddComponent<UserGUI>();
        director.currentSceneController.GenGameObjects();
        this.gameObject.AddComponent<CCActionManager>();
    }
    private void OnEnable()
    {
        ColiAction.myAddScore += AddScore;
        ColiAction.myGameOver += GameOver;
    }
    private void OnDisable()
    {
        ColiAction.myAddScore -= AddScore;
        ColiAction.myGameOver -= GameOver;
    }

    private void GameOver()
    {
        Pause();
        flag = 1;
    }

    private void Start()
    {
    }
    public void GenGameObjects()
    {
        int count = 0;
        GameObject plane = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Plane"));
        plane.transform.parent = this.transform;
        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                if (i == 0 && j == 2)
                {
                    player = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/player"));
                    player.transform.position = new Vector3(plane.transform.position.x + 9 * (i - 1), 0, plane.transform.position.z + 9 * (j - 1));
                    if (player.GetComponent<Rigidbody>())
                    {
                        player.GetComponent<Rigidbody>().freezeRotation = true;
                    }
                }
                else
                {
                    GameObject trigger = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Trigger"));
                    trigger.transform.parent = plane.transform;
                    trigger.transform.position = new Vector3(plane.transform.position.x + 9 * (i - 1), 0, plane.transform.position.z + 10 * (j - 1));
                    trigger.name = "trigger" + count;
                    count++;
                    GameObject patrol = pf.GetPatrol();
                    patrol.transform.position = trigger.transform.position;
                    patrols.Add(patrol);
                }
            }
        }
    }
    public void Restart()
    {
        SceneManager.LoadScene("1");
    }
    public void Pause()
    {
        actionManager.Pause();
    }
    private void AddScore()
    {
        score++;
    }
}

CCActionManager腳本用來管理動作以及用戶輸入:

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

public class CCActionManager : SSActionManager, ISSActionCallback
{
    public FirstSceneController sceneController;
    public List<CCMoveToAction> seq = new List<CCMoveToAction>();
    public UserAction userAction;
    public PatrolFactory pf;

    protected new void Start()
    {
        sceneController = (FirstSceneController)SSDirector.getInstance().currentSceneController;
        userAction = UserAction.GetSSAction(5);
        sceneController.actionManager = this;
        pf = sceneController.pf;
        RunAction(sceneController.player, userAction, this);
        foreach (var i in sceneController.patrols)
        {
            float x = Random.Range(-3.0f, 3.0f);
            int z = Random.Range(0, 4);
            Vector3 target = new Vector3(z % 2 == 0 ? (z - 1) * 3 : x, 0, z % 2 != 0 ? (z - 2) * 3 : x);
            CCMoveToAction k = CCMoveToAction.GetSSAction(target + i.transform.position, 100, i.transform.position);
            seq.Add(k);
            Quaternion rotation = Quaternion.LookRotation(target, Vector3.up);
            i.transform.rotation = rotation;
            RunAction(i, k, this);
        }
    }
    protected new void Update()
    {
        base.Update();
    }
    public void SSActionEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
    {
        if (source != userAction)
        {
            CCMoveToAction cCMoveTo = source as CCMoveToAction;
            float x = Random.Range(-3.0f, 3.0f);
            int z = Random.Range(0, 4);
            Vector3 target = new Vector3(z % 2 == 0 ? (z - 1) * 3.0f : x, 0, z % 2 == 0 ? x : (z - 2) * 3.0f);
            CCMoveToAction k = CCMoveToAction.GetSSAction(target + cCMoveTo.initPosition, 1.5f, cCMoveTo.initPosition);
            seq.Remove(cCMoveTo);
            source.destory = true;
            seq.Add(k);
            Quaternion rotation = Quaternion.LookRotation(target + cCMoveTo.initPosition - source.transform.position, Vector3.up);
            source.transform.rotation = rotation;
            RunAction(source.gameObject, k, this);
        }
    }
    public void CheckEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
    {
    }
    public void Pause()
    {
        if (sceneController.flag == 0)
        {
            foreach (var k in seq)
            {
                k.enable = false;
                k.gameObject.GetComponent<Animator>().SetBool("running", false);
            }
            userAction.enable = false;
            sceneController.flag = 2;
        }
        else if (sceneController.flag == 2)
        {
            foreach (var k in seq)
            {
                k.enable = true;
                k.gameObject.GetComponent<Animator>().SetBool("running", true);
            }
            userAction.enable = true;
            sceneController.flag = 0;
        }
    }
}

UserAction腳本用來處理player的移動:

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

public class UserAction : SSAction
{
    // Use this for initialization
    private float speed;
    private float rspeed = 90;

    public static UserAction GetSSAction(float speed)
    {
        UserAction action = CreateInstance<UserAction>();
        action.speed = speed;
        return action;
    }

    public override void Start()
    {

    }

    public override void Update()
    {
        if (enable)
        {
            float translationX = Input.GetAxis("Horizontal");
            float translationZ = Input.GetAxis("Vertical");
            if (translationX != 0 || translationZ != 0)
            {
                gameObject.GetComponent<Animator>().SetBool("running", true);
            }
            else
            {
                gameObject.GetComponent<Animator>().SetBool("running", false);
            }
            gameObject.transform.Translate(translationX * speed * Time.deltaTime * 0.1f, 0, translationZ * speed * Time.deltaTime);
            gameObject.transform.Rotate(0, translationX * rspeed * Time.deltaTime, 0);
            if (gameObject.transform.localEulerAngles.x != 0 || gameObject.transform.localEulerAngles.z != 0)
            {
                gameObject.transform.localEulerAngles = new Vector3(0, gameObject.transform.localEulerAngles.y, 0);
            }
            if (gameObject.transform.position.y != 0)
            {
                gameObject.transform.position = new Vector3(gameObject.transform.position.x, 0, gameObject.transform.position.z);
            }
        }
    }
}

實現效果

在這裏插入圖片描述

發佈了22 篇原創文章 · 獲贊 1 · 訪問量 6407
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章