0
私は、C#を使用してUnityで2Dのサイドスクロールビデオゲームを作成しています。私は、押された矢印キーが指し示していた方向をプレイヤーに向けさせるスクリプトを作成しました(右矢印が押されたとき、プレイヤーが右を向いているとき、左矢印が押されたとき、プレーヤーは左を向いています)。プレイヤーが直面している方向に発砲体を撃つ方法は?
しかし、プレイヤーが狙っているハープンをプレイヤーが向いている方向に向ける方法を理解できません。私はこのような質問をするStack Overflowに関する多くの質問を見つけましたが、答えはどれも私のために働いていませんでした。
誰でも、プレイヤーがプレイヤーが撃ったハープーンをプレイヤーが向いている方向に向ける方法を教えてください。前もって感謝します!
ここは、私はあなたが既にの方向を知るために、変数facingRightを定義 PLAYER SCRIPT
using UnityEngine;
using System.Collections;
public class playerMove : MonoBehaviour {
// All Variables
public float speed = 10;
private Rigidbody2D rigidBody2D;
private GameObject harpoon_00001;
private bool facingRight = true;
void Awake() {
rigidBody2D = GetComponent<Rigidbody2D>();
harpoon_00001 = GameObject.Find("harpoon_00001");
}
void Update() {
if (Input.GetKeyDown(KeyCode.LeftArrow) && !facingRight) {
Flip();
}
if (Input.GetKeyDown(KeyCode.RightArrow) && facingRight) {
Flip();
}
}
void Flip() {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void FixedUpdate() {
float xMove = Input.GetAxis("Horizontal");
float yMove = Input.GetAxis("Vertical");
float xSpeed = xMove * speed;
float ySpeed = yMove * speed;
Vector2 newVelocity = new Vector2(xSpeed, ySpeed);
rigidBody2D.velocity = newVelocity;
if (Input.GetKeyDown("space")) {
GetComponent<AudioSource>().Play();
Instantiate(harpoon_00001,transform.position,transform.rotation);
}
}
}
HARPOON SCRIPT
using UnityEngine;
using System.Collections;
public class harpoonScript : MonoBehaviour {
// Public variable
public int speed = 6;
private Rigidbody2D r2d;
// Function called once when the bullet is created
void Start() {
// Get the rigidbody component
r2d = GetComponent<Rigidbody2D>();
// Make the bullet move upward
float ySpeed = 0;
float xSpeed = -8;
Vector2 newVelocity = new Vector2(xSpeed, ySpeed);
r2d.velocity = newVelocity;
}
void Update() {
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
float xSpeed = -8;
}
if (Input.GetKeyDown(KeyCode.RightArrow)) {
float xSpeed = 8;
}
}
void OnTriggerEnter2D(Collider2D other) //hero hits side of enemy
{
Destroy(other.gameObject.GetComponent<Collider2D>()); //Remove collider to avoid audio replaying
other.gameObject.GetComponent<Renderer>().enabled = false; //Make object invisible
Destroy(other.gameObject, 0.626f); //Destroy object when audio is done playing, destroying it before will cause the audio to stop
}
}
あなたはハープーンとしてどのような 'ハープーン銛=インスタンス化(harpoon_00001、transform.position、Quaternion.identity)を説明してもらえ;'ていますか?私はC#の初心者です。意味は分かりません。 –
あなたのコードを使用しようとしましたが、Unityは 'Assets/Scripts/playerMove.cs(54,9):error CS0246:タイプまたは名前空間名 'Harpoon 'が見つかりませんでした。あなたはusingディレクティブまたはアセンブリ参照がありませんか?Harpoonはスクリプトの名前ですか?ハープーンはゲームオブジェクトですか? –
ミススペールして申し訳ありません。 Hapoonは実際にあなたのharpoonScriptクラスです。しかし、私はそのようにタイプしました。 –