【问题标题】:Bullets not showing in game view in Unity在 Unity 的游戏视图中未显示子弹
【发布时间】:2021-05-18 09:26:35
【问题描述】:

我正在 Unity 中制作“太空射击”游戏,这是我的问题;

当宇宙飞船开火时,会产生子弹。我可以在层次结构屏幕上看到它们,但在“游戏视图”中看不到子弹。当我切换到“场景视图”并检查它时,我看到子弹在那里并在顶层移动。以下是我到目前为止所做的:

*提高了子弹的排序层(没用)

*升了飞船的排序层(没用)

*我关闭了相机的视差效果(只有黑屏和宇宙飞船,它不起作用)

*我从场景屏幕中检查了图层。 (一切都应该是,但仍然没有工作)

此外,我在场景中放置了我的预制件中的子弹。我在运行游戏时可以看到那个子弹,但是我按空格键射出的子弹是不可见的。

问题截图:

我使用的代码是:

玩家控制器:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5f;
    private float min_Y = -4.2f, max_Y = 4.6f;
    private float min_Z = -.8f, max_Z = -.6f;

    [SerializeField]
    private GameObject PlayerBullet;

    [SerializeField]
    private Transform AttackPoint;

    private void Start()
    {
        
    }

    private void Update()
    {
        MovePlayer();
        Attack();
    }


    void MovePlayer()
    {
        if(Input.GetAxisRaw("Vertical") > 0f)
        {
            Vector3 temp = transform.position;
            Quaternion temp2 = transform.rotation;
            temp.y += speed * Time.deltaTime;
            temp2.z += speed * Time.deltaTime;

            if (temp2.z > max_Z)
                temp2.z = max_Z;

            if (temp.y > max_Y)
                temp.y = max_Y;

            transform.rotation = temp2;
            transform.position = temp;
        }
        else if(Input.GetAxisRaw("Vertical") < 0f)
        {
            Quaternion temp2 = transform.rotation;
            Vector3 temp = transform.position;
            temp.y -= speed * Time.deltaTime;
            temp2.z -= speed * Time.deltaTime;

            if (temp2.z < min_Z)
                temp2.z = min_Z;

            if (temp.y < min_Y)
                temp.y = min_Y;

            transform.rotation = temp2;
            transform.position = temp;
        }
        else
        {
            Quaternion temp2 = transform.rotation;
            if(temp2.z > -0.71f)
            {
                temp2.z -= speed/2 * Time.deltaTime;
                if (temp2.z < -0.71f)
                    temp2.z = -0.71f;
            }
            else if (temp2.z < -0.71f)
            {
                temp2.z += speed/2 * Time.deltaTime;
                if (temp2.z > -0.71f)
                    temp2.z = -0.71f;
            }

            transform.rotation = temp2;
        }
    }

    void Attack()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(PlayerBullet, AttackPoint.position, Quaternion.identity);
        }
    }
}

BulletScript:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletScript : MonoBehaviour
{
    private float speed = 7f;
    private float DeactivateTimer = 3f;
    private void Start()
    {
        
    }

    private void Update()
    {
        Move();
    }

    void Move()
    {
        Vector3 temp = transform.position;
        temp.x += speed * Time.deltaTime;
        transform.position = temp;
    }
}

如果有人可以帮助我,我将不胜感激......

【问题讨论】:

  • 根本不可能是问题的根源,但就像评论一样,所有这些十进制数字的 z 坐标似乎有点奇怪。对我来说,将 az 保留为像 -10 或 0 这样的整数会更有意义。我还会尝试让 Z 明显更靠近相机(如相机和背景之间的 -5)来检查子弹是否显示在游戏屏幕上,如果场景中对象的 z 坐标可能有问题。
  • 这样使用 Z 坐标的原因是为了让飞船在向上或向下移动时“倾斜”在那个方向上。倾斜效果对子弹没有影响,因为我发射的子弹来自宇宙飞船前面的游戏对象。
  • 请使用正确的标签! unityscript 是或更好的是 JavaScript 风格,类似于早期 Unity 版本中使用的自定义语言,现在早已弃用!你的脚本显然是c#

标签: c# unity3d game-engine


【解决方案1】:

解决了!创建的子弹在 transform.z 中有 -9。修复了它用 0 替换它们。真是个愚蠢的 msitake...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 2020-06-05
    相关资源
    最近更新 更多