2016-07-04 13 views
0

Ich erstelle ein 2D-Side-Scroller-Videospiel in Unity mit C#. Ich habe das Skript erstellt, das den Spieler in die Richtung zeigt, in die die Pfeiltaste, auf die gedrückt wurde, zeigte (wenn der rechte Pfeil gedrückt wird, zeigt der Spieler nach rechts. Wenn der linke Pfeil gedrückt wird, zeigt der Spieler nach links).Wie schieße ich ein Projektil in die Richtung, in die der Spieler blickt?

Allerdings kann ich nicht herausfinden, wie man die Harpune, die der Spieler schießt, in die Richtung richtet, in die der Spieler blickt. Ich habe viele Fragen zu Stack Overflow gestellt, die Fragen wie diese stellen, aber keine ihrer Antworten hat für mich funktioniert.

Kann mir bitte jemand sagen, wie man die Harpune, die der Spieler schießt, in die Richtung sieht, in die der Spieler blickt? Danke im Voraus!

Hier ist mein Code, dass ich 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 

} 

} 

Antwort

0

Sie bereits, die eine variable facingRight kennen, um die Richtung der die bedienen- Spieler. Sie können dieses Wissen verwenden, um die Harpune zu kontrollieren. Zum Beispiel:

// this line creates a new object, which has harpoonScript attached to it. 
// In unity editor, you drag and drop this prefab(harpoon_00001) into right place. 
// transform.position is used for the starting point of the fire. You can also add +-some_vector3 for better placement 
// Quaternion.identity means no rotation. 
harpoonScript harpoon = Instantiate(harpoon_00001,transform.position, Quaternion.identity) as harpoonScript; 
// Assuming harpoon prefab already facing to right 
if (!facingRight) { 
    // Maybe, not required 
    harpoon.transform.eulerAngles = new Vector3(0f, 0f, 180f); // Face backward 
    Vector3 theScale = harpoon.transform.localScale; 
    theScale.y *= -1; 
    harpoon.transform.localScale = theScale; // Flip on y axis 
} 
+0

Könnten Sie bitte erklären, was 'Harpune Harpune = Instantiate (harpoon_00001, transform.position, Quaternion.identity) als Harpune;' tut? Ich bin neu in C#, und ich bin mir nicht sicher, was ich verstehe –

+0

Ich habe versucht, Ihren Code zu verwenden, aber Unity sagt, dass Assets/Scripts/playerMove.cs (54,9): Fehler CS0246: Der Typ oder Namensraumname "Harpoon" konnte nicht gefunden werden. Vermissen Sie eine using-Direktive oder eine Assembly-Referenz? 'Ist Harpoon der Name eines Skripts? Ist Harpoon ein GameObject? –

+0

Entschuldigung für das Rechtschreibfehler. Hapoon ist eigentlich deine HarpoonScript-Klasse. Aber ich tippte nur so. –