欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity實現(xiàn)3D射箭小游戲

 更新時間:2021年04月23日 14:24:42   作者:lihan_96  
這篇文章主要為大家詳細介紹了Unity實現(xiàn)3D射箭小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Unity 小游戲:3D射箭,供大家參考,具體內(nèi)容如下

前兩周因為實訓太忙,再加上自己對老師所講的設計模式并不是很理解,所以就沒有寫博客。這次博客是記錄3D射箭游戲的實現(xiàn)過程。

1. 準備資源

我是在網(wǎng)上找的弓與箭的資源,至于靶子,創(chuàng)建五個不同大小的同心圓柱體,如圖所示:

需要注意的是,五個圓柱體并不在同一個平面上,這樣才能夠看清每一環(huán)的顏色,并且在檢測碰撞時不會出現(xiàn)各種問題。

另外,如果靶子放得離相機太近,就沒有射箭的感覺了;離相機太遠,好像又看不清靶子了,然后我試著把靶子MaterialShader改為 Sprites/Default ,這樣靶子離相機遠一點也能看得很清晰。

2. 布置場景

把弓箭作為Main Camera的子物體,這樣我們可以在用鼠標控制鏡頭移動時,使弓箭一直指向屏幕中心,以達到第一人稱控制器的效果。

在此項目中,沒有選擇使用GUI來做UI界面,而是創(chuàng)建了一個Canvas,在這里面添加了一個Image用來顯示弓箭的準心,以及四個Text來顯示得分、風向、風力、提示等。

3. 編輯腳本

游戲采用MVC架構(gòu),大部分功能是自己實現(xiàn)的,也有一小些函數(shù)是借鑒大神的。整體上感覺有很多缺陷,但是又不知道怎么修改才好,我也很無奈?。 ?°ˊДˋ°) °

下面是我的UML圖:

以下是完整代碼:

SSDirector.cs

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

public class SSDirector : System.Object {

    private static SSDirector _instance;

    public ISceneCotroller currentScenceCotroller {
        get;
        set;
    }

    public bool running {
        get;
        set;
    }

    public static SSDirector getInstance() {
        if (_instance == null) {
            _instance = new SSDirector ();
        }
        return _instance;
    }
}

ISceneCotroller.cs

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

public interface ISceneCotroller {
    void LoadResources ();
}

IUserAction.cs

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

public interface IUserAction {
    string getMyScore ();
    float getWind ();
    void Openbow ();
    void Draw ();
    void Shoot ();
}

ActionManager.cs

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

public class ActionManager : MonoBehaviour {

    private float Force = 0f;
    private int maxPower = 2500;
    private float power;
    public Transform arrowSpawn;
    public Transform myArrow;
    public Transform bow;

    //播放拉弓動畫
    public void Openbow () {
        bow.GetComponent<Animation>().Play("Draw");
        bow.GetComponent<Animation>()["Draw"].speed = 1;
        bow.GetComponent<Animation>()["Draw"].wrapMode = WrapMode.Once;

        arrowSpawn.GetComponent<MeshRenderer>().enabled = true;

        //重置 power 為 0
        power = 0;
    }

    //拉弓,從power為0到power為3000
    public void Draw () {
        if(power < maxPower) {
            power += maxPower * Time.deltaTime;
        }
    }

    //射箭
    public void Shoot () {
        float percent = bow.GetComponent<Animation>()["Draw"].time / bow.GetComponent<Animation>()["Draw"].length;
        float shootTime = 1 * percent;

        bow.GetComponent<Animation>().Play("Shoot");
        bow.GetComponent<Animation>()["Shoot"].speed = 1;
        bow.GetComponent<Animation>()["Shoot"].time = shootTime;
        bow.GetComponent<Animation>()["Shoot"].wrapMode = WrapMode.Once;

        arrowSpawn.GetComponent<MeshRenderer>().enabled = false;
        Transform arrow= Instantiate (myArrow, arrowSpawn.transform.position, transform.rotation);
        arrow.transform.GetComponent<Rigidbody>().AddForce(transform.forward * power);

        wind (arrow);
        Force = Random.Range (-100, 100);
    }

    //產(chǎn)生風
    private void wind(Transform arrow) {
        arrow.transform.GetComponent<Rigidbody> ().AddForce (new Vector3 (Force, 0, 0), ForceMode.Force);
    }

    //返回風
    public float getWindForce() {
        return Force;
    }  
}

ScoreRecorder.cs

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

public class ScoreRecorder : MonoBehaviour {

    private string Score = "0";

    //判斷得分
    public void countScore(string type) {
        Score = type;
    }

    //返回分數(shù)
    public string getScore () {
        return Score;
    }
}

FirstScene.cs

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

public class FirstScene : MonoBehaviour, ISceneCotroller, IUserAction {

    private ActionManager actionManager;
    private ScoreRecorder scoreRecorder;

    void Awake () {
        SSDirector director = SSDirector.getInstance ();
        director.currentScenceCotroller = this;
        director.currentScenceCotroller.LoadResources ();
        actionManager = (ActionManager)FindObjectOfType (typeof(ActionManager));
        scoreRecorder = (ScoreRecorder)FindObjectOfType (typeof(ScoreRecorder));
    }

    //加載預制物體靶子
    public void LoadResources () {
        Debug.Log ("loading...\n");
        GameObject target = Instantiate<GameObject> (
                                Resources.Load<GameObject> ("Prefabs/target"));
        target.name = "target";
    }

    //獲得分數(shù)
    public string getMyScore () {
        return scoreRecorder.getScore ();
    }

    //獲得風向和風力
    public float getWind () {
        return actionManager.getWindForce ();
    }

    //拉弓
    public void Openbow () {
        actionManager.Openbow ();
    }

    //蓄力
    public void Draw () {
        actionManager.Draw ();
    }

    //射箭
    public void Shoot () {
        actionManager.Shoot ();
    }
}

UserGUI.cs

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

public class UserGUI : MonoBehaviour {

    private IUserAction userAction;
    private FirstScene scene;
    private Quaternion m_CharacterTargetRot;

    public Text Score;
    public Text WindDirection;
    public Text WindForce;

    // Use this for initialization
    void Start () {
        userAction = SSDirector.getInstance ().currentScenceCotroller as IUserAction;
    }

    void Awake () {
        m_CharacterTargetRot = transform.localRotation;
    }

    void Update () {
        //鏡頭跟隨鼠標
        float xRot = Input.GetAxis ("Mouse X") * 3f;
        float yRot = Input.GetAxis ("Mouse Y") * -3f;
        m_CharacterTargetRot *= Quaternion.Euler (yRot, xRot, 0f);
        transform.localRotation = Quaternion.Slerp (transform.localRotation, m_CharacterTargetRot,
            5f * Time.deltaTime);

        //按空格鍵使弓箭瞄準靶心
        if (Input.GetKeyDown (KeyCode.Space)) {
            m_CharacterTargetRot = Quaternion.Euler (0f, 0f, 0f);
            transform.localRotation = Quaternion.Slerp (transform.localRotation, m_CharacterTargetRot,
                5f * Time.deltaTime);
        }

        //鼠標左鍵按下,開始拉弓
        if (Input.GetMouseButtonDown (0)) {
            userAction.Openbow ();
        }

        //鼠標左鍵按住不放,蓄力
        if (Input.GetMouseButton (0)) {
            userAction.Draw ();
        }

        //鼠標左鍵抬起。射箭
        if (Input.GetMouseButtonUp (0)) {
            userAction.Shoot ();
        }

        Score.text = "Score : " + userAction.getMyScore ();     //顯示上一輪分數(shù)
        float force = userAction.getWind ();
        if (force < 0) {
            WindDirection.text = "Wind Direction : <---";       //顯示風向
        } else if (force > 0) {
            WindDirection.text = "Wind Direction : --->";
        } else {
            WindDirection.text = "Wind Direction : No";
        }
        WindForce.text = "Wind Force : " + Mathf.Abs (userAction.getWind ()); //顯示風力
    }
}

Arrow.cs

using UnityEngine;
using System.Collections;

public class Arrow : MonoBehaviour {

    private RaycastHit hit;

    void  Update (){
        //檢測在移動的箭
        if(GetComponent<Rigidbody>().velocity.magnitude > 0.5f) {
            CheckForHit();
        } else {
            enabled = false;
        }
        if (transform.position.y < -5) {
            Destroy (this.gameObject);      //將掉出地面以下的箭銷毀
        }
    }

    //檢測是否碰撞
    void CheckForHit (){
        float myVelocity = GetComponent<Rigidbody>().velocity.magnitude;
        float raycastLength = myVelocity * 0.03f;

        if(Physics.Raycast(transform.position, transform.forward, out hit, raycastLength)) {
            GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;     //使箭停留在靶子上
            transform.position = hit.point;
            transform.parent = hit.transform;
            enabled = false;
        } else {
            Quaternion newRot = transform.rotation;
            newRot.SetLookRotation(GetComponent<Rigidbody>().velocity);
            transform.rotation = newRot;    //箭沒中靶,則繼續(xù)做拋物線運動
        }
    }
}

Target.cs

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

public class Target : MonoBehaviour {

    private ScoreRecorder scoreRecorder;

    public string score;    //對應靶環(huán)的分數(shù)

    public void Start() {
        scoreRecorder = (ScoreRecorder)FindObjectOfType (typeof(ScoreRecorder));
    }

    void OnTriggerEnter(Collider other) {
        if (other.gameObject.tag == "Arrow") {
            scoreRecorder.countScore (score);       //記錄分數(shù)
        }
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#實現(xiàn)簡單的字符串加密

    C#實現(xiàn)簡單的字符串加密

    這篇文章介紹了C#實現(xiàn)字符串加密的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C# wpf使用ListBox實現(xiàn)尺子控件的示例代碼

    C# wpf使用ListBox實現(xiàn)尺子控件的示例代碼

    本文主要介紹了C# wpf使用ListBox實現(xiàn)尺子控件的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • C#編寫COM組件的方法分析

    C#編寫COM組件的方法分析

    這篇文章主要介紹了C#編寫COM組件的方法,結(jié)合實例形式分析了C#編寫COM組件的具體步驟與相關實現(xiàn)技巧,需要的朋友可以參考下
    2017-06-06
  • C#如何給PDF文件添加水印

    C#如何給PDF文件添加水印

    這篇文章主要為大家詳細介紹了C#如何給PDF文件添加水印的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • C#軟件注冊碼的實現(xiàn)代碼

    C#軟件注冊碼的實現(xiàn)代碼

    開發(fā)軟件時,當用到商業(yè)用途時,注冊碼與激活碼就顯得很重要了,現(xiàn)在的軟件技術實在在強了,各種國內(nèi)外大型軟件都有注冊機制,但我們學習的是技術
    2013-05-05
  • C#實現(xiàn)拷貝文件的9種方法小結(jié)

    C#實現(xiàn)拷貝文件的9種方法小結(jié)

    最近遇一個問題,一個程序調(diào)用另一個程序的文件,結(jié)果另一個程序的文件被占用,使用不了文件,這時候的解決方案就是把另一個程序的文件拷貝到當前程序就可以了,本文介紹用C#拷貝文件的多種方式,需要的朋友可以參考下
    2024-04-04
  • C#實現(xiàn)加密的幾種方法介紹

    C#實現(xiàn)加密的幾種方法介紹

    這篇文章介紹了C#實現(xiàn)加密的幾種方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#反射(Reflection)詳解

    C#反射(Reflection)詳解

    本文詳細講解了C#中的反射(Reflection),文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 深入學習C#網(wǎng)絡編程之HTTP應用編程(下)

    深入學習C#網(wǎng)絡編程之HTTP應用編程(下)

    這篇文章主要介紹了深入學習C#網(wǎng)絡編程之HTTP應用編程的相關知識,文中講解的非常詳細,幫助大家更好的學習c#網(wǎng)絡編程,感興趣的朋友可以了解下
    2020-06-06
  • C#中查找Dictionary中的重復值的方法

    C#中查找Dictionary中的重復值的方法

    這篇文章主要介紹了C#中查找Dictionary中的重復值的方法,需要的朋友可以參考下
    2015-09-09

最新評論