C# 如何向玩家面对的方向发射射弹?

C# 如何向玩家面对的方向发射射弹?,c#,unity3d,game-physics,unity5,projectile,C#,Unity3d,Game Physics,Unity5,Projectile,我正在用c#在Unity中创建一个2D侧滚视频游戏。我已经创建了一个脚本,使玩家面向被按下的箭头键所指向的方向(当按下右箭头时,玩家面向右。当按下左箭头时,玩家面向左) 然而,我不知道如何使玩家射击的鱼叉指向玩家面对的方向。我在Stack Overflow上发现了很多这样的问题,但他们的答案都不适合我 有人能告诉我如何使玩家射击的鱼叉朝向玩家所面对的方向吗?提前谢谢 这是我使用的代码- 玩家脚本 using UnityEngine; using System.Collections; publ

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

}

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

}

}
使用UnityEngine;
使用系统集合;
公共类玩家运动:单一行为{
//所有变量
公共浮动速度=10;
私有刚体2d刚体2d;
私人游戏对象鱼叉_00001;
私有bool-facingRight=true;
无效唤醒(){
rigidBody2D=GetComponent();
harpoon_00001=GameObject.Find(“harpoon_00001”);
}
无效更新(){
if(Input.GetKeyDown(KeyCode.LeftArrow)&&!facingRight){
翻转();
}
if(Input.GetKeyDown(KeyCode.RightArrow)&&facingRight){
翻转();
}
}
无效翻转(){
facingRight=!facingRight;
Vector3 theScale=transform.localScale;
比例x*=-1;
transform.localScale=比例;
}
无效固定更新(){
float xMove=Input.GetAxis(“水平”);
float yMove=Input.GetAxis(“垂直”);
浮动X速度=X移动*速度;
浮动Y速度=Y移动*速度;
Vector2 newVelocity=新Vector2(xSpeed,ySpeed);
rigidBody2D.velocity=新速度;
if(Input.GetKeyDown(“空格”)){
GetComponent().Play();
实例化(鱼叉_00001,变换.位置,变换.旋转);
}
} 
}
鱼叉脚本

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);

}

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

}

}
使用UnityEngine;
使用系统集合;
公共类鱼叉脚本:MonoBehavior{
//公共变量
公共int速度=6;
私有刚体2d r2d;
//创建项目符号时调用一次函数
无效开始(){
//获取刚体组件
r2d=GetComponent();
//使子弹向上移动
浮动Y速度=0;
浮点xSpeed=-8;
Vector2 newVelocity=新Vector2(xSpeed,ySpeed);
r2d.速度=新速度;
}
无效更新(){
if(Input.GetKeyDown(KeyCode.LeftArrow)){
浮点xSpeed=-8;
}
if(Input.GetKeyDown(KeyCode.RightArrow)){
浮动xSpeed=8;
}
}
void OnTriggerEnter2D(碰撞其他)//英雄击中敌人一侧
{
销毁(other.gameObject.GetComponent());//删除碰撞器以避免音频重放
other.gameObject.GetComponent().enabled=false;//使对象不可见
销毁(other.gameObject,0.626f);//音频播放结束后销毁对象,之前销毁对象将导致音频停止
}
}

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

请您解释一下什么
Harpoon-Harpoon=实例化(Harpoon_00001,transform.position,Quaternion.identity)为Harpoon是什么?我是c#的新手,我不确定我是否理解这意味着我试图使用你的代码,但Unity说
Assets/Scripts/playerMove.cs(54,9):错误CS0246:找不到类型或命名空间名称
Harpoon'。您是否缺少using指令或程序集引用?` Harpoon是脚本的名称吗?鱼叉是游戏对象吗?很抱歉拼错了。Hapoon实际上是您的鱼叉脚本类。但是,我只是这样键入。我没有任何错误,但鱼叉仍然没有正确的方向,并且每次按enter键时,检查器都会说
NullReferenceException:Object reference没有设置为Object playerMove.FixedUpdate()(在Assets/Scripts/playerMove.cs:60)的实例
。我想我仍然缺少一些东西…你需要在编辑处分配它。您知道,在playerScript的inspector窗口中的正确位置拖放鱼叉脚本的预置。如果你对什么是预制或检查窗口有问题,我建议你看一本关于unity的不错的教程。另外,如果您在这里搜索NullReferenceException和unity,您可以找到问题所在。