【问题标题】:How Do I Shoot a Projectile into the Direction the Player is Facing?如何将弹丸射向玩家面对的方向?
【发布时间】:2016-07-04 01:57:02
【问题描述】:

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

}

}

【问题讨论】:

    标签: c# unity3d game-physics unity5 projectile


    【解决方案1】:

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

    // 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 = Instantiate(harpoon_00001,transform.position, Quaternion.identity) as Harpoon; 的作用吗?我是 c# 新手,我不确定我理解它的含义
    • 我尝试使用您的代码,但 Unity 说找不到 Assets/Scripts/playerMove.cs(54,9): error CS0246: The type or namespace name Harpoon'。您是否缺少 using 指令或程序集引用?` Harpoon 是脚本的名称吗?鱼叉是游戏对象吗?
    • 抱歉拼错了。 Hapoon 实际上是你的 harpoonScript 类。但是,我只是这样输入的。
    • 我没有任何错误,但鱼叉仍然没有走正确的路,每次我按回车时检查员都会说NullReferenceException: Object reference not set to an instance of an object playerMove.FixedUpdate () (at Assets/Scripts/playerMove.cs:60)。我认为我仍然缺少一些东西......
    • 您需要在编辑器中分配它。您知道,将 harpoonScript 的预制件拖放到 playerScript 的检查器窗口中的正确位置。如果您对什么是预制件或检查器窗口有疑问,我建议您观看有关统一的体面教程。另外,如果你在这里搜索NullReferenceException和unity,你可以找出问题所在。
    猜你喜欢
    • 2014-06-21
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多