【问题标题】:How to shoot in Unity in both directions?如何在Unity中双向拍摄?
【发布时间】:2018-05-13 07:01:07
【问题描述】:

我对 Unity 很陌生,在过去也没有使用过 Unity 之前,我从来没有用 C# 编写过代码。我只知道Java和Python。我从高中四年级开始就知道 Java,现在我是大学二年级的学生。我制作了我的项目,其中包含我的人类角色,它应该向 12 只美洲虎发射子弹。我已经从 Unity 提供的 2d UFO 示例中实现了所有这些。我的子弹有一张 png 图片。我真的不知道如何实现子弹。我需要让子弹从两个方向射出。我逐渐开始学习如何基本拍摄(一个方向)。我参考了这个链接并将其添加到我的程序中,添加项目符号代码后它不会编译。正如网站所说,我在 Unity 程序中创建了一个球体,并将图像设置为 png 项目符号。

https://unity3d.com/learn/tutorials/temas/multiplayer-networking/shooting-single-player

现在这是我的代码。我顺便下载了免费的最新版 Unity。顺便说一下,我的游戏是 2D 的。这只是漫画。

using UnityEngine;
using System.Collections;

//Adding this allows us to access members of the UI namespace including Text.
using UnityEngine.UI;

public class CompletePlayerController : MonoBehaviour {

    public float speed;             //Floating point variable to store the player's movement speed.
    public Text countText;          //Store a reference to the UI Text component which will display the number of pickups collected.
    public Text winText;            //Store a reference to the UI Text component which will display the 'You win' message.

    private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
    private int count;              //Integer to store the number of pickups collected so far.

    public GameObject bulletPrefab;

    public Transform bulletSpawn;

    // Use this for initialization
    void Start()
    {
        //Get and store a reference to the Rigidbody2D component so that we can access it.
        rb2d = GetComponent<Rigidbody2D> ();

        //Initialize count to zero.
        count = 0;

        //Initialze winText to a blank string since we haven't won yet at beginning.
        winText.text = "";

        //Call our SetCountText function which will update the text with the current value for count.
        SetCountText ();
    }

    //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
    void FixedUpdate()
    {
        //Store the current horizontal input in the float moveHorizontal.
        float moveHorizontal = Input.GetAxis ("Horizontal");

        //Store the current vertical input in the float moveVertical.
        float moveVertical = Input.GetAxis ("Vertical");

        //Use the two store floats to create a new Vector2 variable movement.
        Vector2 movement = new Vector2 (moveHorizontal, moveVertical);

        //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
        rb2d.AddForce (movement * speed);

        if (!isLocalPlayer)
        {
            return;
        }

        var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
        var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;

        transform.Rotate(0, x, 0);
        transform.Translate(0, 0, z);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Fire();
        }


    }

    void Fire()
    {
    // Create the Bullet from the Bullet Prefab
        var bullet = (GameObject)Instantiate (
        bulletPrefab,
        bulletSpawn.position,
        bulletSpawn.rotation);

    // Add velocity to the bullet
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;

    // Destroy the bullet after 2 seconds
        Destroy(bullet, 2.0f);
    }

     public override void OnStartLocalPlayer ()
    {
        GetComponent<MeshRenderer>().material.color = Color.blue;
    }


    //OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
    void OnTriggerEnter2D(Collider2D other) 
    {
        //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
        if (other.gameObject.CompareTag ("PickUp")) 
        {
            //... then set the other object we just collided with to inactive.
            other.gameObject.SetActive(false);

            transform.localScale += new Vector3(0.1f, 0.1f, 0);

            //Add one to the current value of our count variable.
            count = count + 1;

            //Update the currently displayed count by calling the SetCountText function.
            SetCountText ();
        }


    }

    //This function updates the text displaying the number of objects we've collected and displays our victory message if we've collected all of them.
    void SetCountText()
    {
        //Set the text property of our our countText object to "Count: " followed by the number stored in our count variable.
        countText.text = "Count: " + count.ToString ();

        //Check if we've collected all 12 pickups. If we have...
        if (count >= 12)
            //... then set the text property of our winText object to "You win!"
            winText.text = "You win!";
    }
}

其他类

using UnityEngine;
using System.Collections;

public class CompleteCameraController : MonoBehaviour {

    public GameObject player;       //Public variable to store a reference to the player game object


    private Vector3 offset;         //Private variable to store the offset distance between the player and camera

    // Use this for initialization
    void Start () 
    {
        //Calculate and store the offset value by getting the distance between the player's position and camera's position.
        offset = transform.position - player.transform.position;
    }

    // LateUpdate is called after Update each frame
    void LateUpdate () 
    {
        // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
        transform.position = player.transform.position + offset;
    }
}

【问题讨论】:

  • 首先,听起来您有一个用于射击子弹的工作代码,并且您还需要在射击第一颗子弹的同时向其他方向射击另一颗子弹。但是,在文本中您说“我真的不知道如何实现子弹”。那么,您的问题是什么?
  • @rs232 我的问题是如何让我的子弹发挥作用?我已经在 Unity 中实现了预制件,我必须获得子弹编码才能让子弹射击。我的代码没有编译。
  • @potanta 如果您制作的 2D 游戏不使用 3D 对象,那么刚体也是如此 - 您也应该使用刚体 2d 作为子弹。您链接的教程适用于 3d
  • @potanta 您必须将此脚本作为组件附加到场景中的某个游戏对象,而不是您的代码将自动编译,您无法自己编译。
  • 请澄清您的问题。此外,如果您在编译时遇到错误,也请分享。

标签: c# unity3d unity5


【解决方案1】:

您需要按照您分享的教程链接中的所有步骤进行操作。看起来您已经复制了一些代码并在控制器类中使用了它。 OnStartLocalPlayer 被标记为被覆盖,但它不在 Monobehaviour 中。它在 NetworkBehaviour 中定义。因此,您有一个错误。正如其他 cmets 中提到的,控制台错误会告诉您问题出在哪里。

【讨论】:

  • 那我应该把它改成网络行为吗?我这样做了,它仍然没有编译。此外,Fiffe 的评论说我提供的链接仅适用于 3d。现在我被困住了。刚才,我添加了一个二维圆形对撞机以及一个二维刚体,就像 Fiffe 说的那样。
  • 编译时说找不到网络行为。它的意思是“您是否缺少任何程序集?”
  • 既然你知道python,你必须知道要使用一个模块,你需要先导入它。在 C# 中也是如此。您需要使用 UnityEngine.Networking 导入命名空间。由于您的要求不是开发多人游戏,因此您可以删除 OnStartLocalPlayer 方法并将 NetworkBehaviour 更改为 MonoBehaviour。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-19
相关资源
最近更新 更多