如何将射弹射入玩家面向的方向?

时间:2016-07-04 01:57:02

标签: c# unity3d game-physics unity5 projectile

我正在使用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);

}

} 
}

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

}

}

1 个答案:

答案 0 :(得分:0)

您已经定义了一个变量 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
}