简体   繁体   English

如何将弹丸射向玩家所面对的方向?

[英]How Do I Shoot a Projectile into the Direction the Player is Facing?

I am creating a 2D side-scroller video game in Unity using c#. 我正在使用c#在Unity中创建2D侧滚动视频游戏。 I have created the script that makes the player face the direction that the arrow key that was pressed was pointing to (when the right arrow is pressed, the player faces right. When the left arrow is pressed, the player faces left). 我创建了一个脚本,使玩家面对被按下的箭头键指向的方向(当按下向右箭头时,玩家面对右。当按下向左箭头时,玩家面向左)。

However, I cannot figure out how to make the harpoon that the player shoots point in the direction the player is facing. 但是,我无法弄清楚如何使玩家射击的鱼叉指向玩家所面对的方向。 I have found many questions on Stack Overflow asking questions like this, but none of their answers worked for me. 我在Stack Overflow上发现了很多这样的问题,但他们的答案都对我没有用。

Can anyone please tell me how to make the harpoon the player shoots face the direction the player is facing? 谁能告诉我如何使玩家射击的鱼叉面对玩家面对的方向? Thanks in advance! 提前致谢!

Here is my code that I use- PLAYER SCRIPT 这是我使用的代码-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

}

}

You already defining a variable facingRight to know the direction of the the player. 您已经定义了一个变量FaceingRight来知道播放器的方向。 You can use that knowledge to control the harpoon. 您可以使用该知识来控制鱼叉。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM