ユニティは3 Dアーチェリーのミニゲームを実現します。
ユニティゲーム:3 Dアーチェリー、参考にしてください。具体的な内容は以下の通りです。
前の二週間は研修が忙しかった上に、先生の言ったデザインパターンがよく分かりませんでしたので、ブログを書きませんでした。今回のブログは3 Dアーチェリーゲームの実現過程を記録しています。
1.リソースの準備
私はネットで探していた弓と矢の資源です。標的となると、五つの異なる大きさの同心円柱を作ります。
5つの円柱は同じ平面ではないので、各リングの色が見え、衝突を検出する際には様々な問題が発生しないことに注意したい。
また、ターゲットがカメラに近すぎると、矢を射る感じがなくなります。カメラから遠すぎて、また標的が見えなくなりました。そしてターゲットのマットのShaderをSprites/Defaultに変えてみます。このようにターゲットはカメラから遠くてもよく見えます。
2.配置シーン
弓をMain Cameraのサブオブジェクトとして使うと、マウスでレンズを移動させながら、弓矢をスクリーンの中心にまっすぐに向けて、一人称コントローラの効果を達成できます。
このプロジェクトでは、GUIを使ってUIを行うことを選択せずに、Canvasを作成しました。この中に弓矢の準心を表示するためのImageと、4つのTextを追加しました。得点、風向き、風力、ヒントなどを表示します。
3.スクリプトの編集
ゲームはMVCの構造を採用して、大部分の機能は自分で実現したので、いくつかの関数も参考にしたのです。全体的に多くの欠陥を感じていますが、どうやって修正すればいいのか分かりません。°(°@ДA°)°
以下は私のUML図です。
以下は完全コードです。
SSDirector.cs
前の二週間は研修が忙しかった上に、先生の言ったデザインパターンがよく分かりませんでしたので、ブログを書きませんでした。今回のブログは3 Dアーチェリーゲームの実現過程を記録しています。
1.リソースの準備
私はネットで探していた弓と矢の資源です。標的となると、五つの異なる大きさの同心円柱を作ります。
5つの円柱は同じ平面ではないので、各リングの色が見え、衝突を検出する際には様々な問題が発生しないことに注意したい。
また、ターゲットがカメラに近すぎると、矢を射る感じがなくなります。カメラから遠すぎて、また標的が見えなくなりました。そしてターゲットのマットのShaderをSprites/Defaultに変えてみます。このようにターゲットはカメラから遠くてもよく見えます。
2.配置シーン
弓をMain Cameraのサブオブジェクトとして使うと、マウスでレンズを移動させながら、弓矢をスクリーンの中心にまっすぐに向けて、一人称コントローラの効果を達成できます。
このプロジェクトでは、GUIを使ってUIを行うことを選択せずに、Canvasを作成しました。この中に弓矢の準心を表示するためのImageと、4つのTextを追加しました。得点、風向き、風力、ヒントなどを表示します。
3.スクリプトの編集
ゲームはMVCの構造を採用して、大部分の機能は自分で実現したので、いくつかの関数も参考にしたのです。全体的に多くの欠陥を感じていますが、どうやって修正すればいいのか分かりません。°(°@ДA°)°
以下は私の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;
}
}
ISceneCotrolller.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 ();
}
アクションManager.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);
}
//
private void wind(Transform arrow) {
arrow.transform.GetComponent<Rigidbody> ().AddForce (new Vector3 (Force, 0, 0), ForceMode.Force);
}
//
public float getWindForce() {
return Force;
}
}
スコアレーダ.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;
}
//
public string getScore () {
return Score;
}
}
First Scene.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...
");
GameObject target = Instantiate<GameObject> (
Resources.Load<GameObject> ("Prefabs/target"));
target.name = "target";
}
//
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.
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 (); //
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 ()); //
}
}
Arow.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; // ,
}
}
}
Target.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Target : MonoBehaviour {
private ScoreRecorder scoreRecorder;
public string score; //
public void Start() {
scoreRecorder = (ScoreRecorder)FindObjectOfType (typeof(ScoreRecorder));
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Arrow") {
scoreRecorder.countScore (score); //
}
}
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。