我如何向玩家面对的方向发射炮弹?

本文关键字:发射 方向 面对 玩家 | 更新日期: 2023-09-27 17:54:15

我正在使用c#在Unity中创建一个2D横向卷轴视频游戏。我已经创建了一个脚本,使玩家面对箭头键所指向的方向(当右箭头被按下时,玩家面向右)。当按下左箭头时,玩家面向左边)。

然而,我不知道如何让玩家射出的鱼叉指向玩家面对的方向。我在Stack Overflow上发现了很多这样的问题,但是他们的答案都不适合我。

谁能告诉我如何使鱼叉玩家射击面对玩家面对的方向?提前感谢!

这是我使用-的代码玩家脚本

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);
}
} 
}
<<p> 鱼叉脚本/strong>
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
}
}

我如何向玩家面对的方向发射炮弹?

你已经定义了一个变量facingRight来了解玩家的方向。你可以利用这些知识来控制鱼叉。例如:

// 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
}