【问题标题】:Unity - Aim Line (Line Renderer) not showing in 2D shooterUnity - 目标线(线渲染器)未在 2D 射击游戏中显示
【发布时间】:2019-06-30 20:22:05
【问题描述】:

我是 Unity 新手,我正在为一个学校项目创建一个小型 2D 射击游戏,我已经设法在主角中拥有一个功能性射击系统,它甚至可以使用 Unity 物理从一些物体上反弹。

我现在想集成一条可以预测子弹轨迹的瞄准线,包括物体上的反弹(泡泡射击风格)。

我发现了一个使用 Raycast 和 Line Renderer 的脚本,据说它可以做到这一点,我尝试将它集成到我的枪脚本中,但虽然它没有给出任何错误,但在我测试游戏时它根本没有显示任何内容。我不知道问题出在我放入 Line Renderer 组件的设置中还是在脚本中。

有人可以帮助我了解我的错误在哪里并指出正确的方法吗?

我的目标是:

我的线渲染器组件定义:

我的武器脚本:

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

[RequireComponent(typeof(LineRenderer))]
public class Weapon : MonoBehaviour
{
[Range(1, 5)]
[SerializeField] private int _maxIterations = 3;
[SerializeField] private float _maxDistance = 10f;

public int _count;
public LineRenderer _line;

public Transform Firepoint;
public GameObject BulletPrefab;
public GameObject FirePrefab;

void Start()
{
    _line = GetComponent<LineRenderer>();
}

// Update is called once per frame
 void Update()
{
  if (Input.GetButtonDown("Fire1"))
    {
        Shoot();
    }

    _count = 0;
    _line.SetVertexCount(1);
    _line.SetPosition(0, transform.position);
    _line.enabled = RayCast(new Ray(transform.position, transform.forward));
}

void Shoot()
{
    //shooting logic
    var destroyBullet = Instantiate(BulletPrefab, Firepoint.position, Firepoint.rotation);
    Destroy(destroyBullet, 10f);
    var destroyFire = Instantiate(FirePrefab, Firepoint.position, Firepoint.rotation);
    Destroy(destroyFire, 0.3f);
}

private bool RayCast(Ray ray)
{
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, _maxDistance) && _count <= _maxIterations - 1)
    {
        _count++;
        var reflectAngle = Vector3.Reflect(ray.direction, hit.normal);
        _line.SetVertexCount(_count + 1);
        _line.SetPosition(_count, hit.point);
        RayCast(new Ray(hit.point, reflectAngle));
        return true;
    }
    _line.SetVertexCount(_count + 2);
    _line.SetPosition(_count + 1, ray.GetPoint(_maxDistance));
    return false;
}
}

【问题讨论】:

    标签: unity3d 2d game-physics 2d-games raycasting


    【解决方案1】:

    Unity 有两个物理引擎,一个用于 2D,一个用于 3D。您提供的脚本依赖于 3D 物理引擎,不适用于 2D 对撞机。我编辑了您的脚本以使用 2D 对撞机。

    无论哪种方式,请确保您的所有游戏对象都使用相同的物理系统。此外,原始脚本仅在碰到某些东西时才显示线渲染器。祝你好运!

    using UnityEngine;
    
    [RequireComponent(typeof(LineRenderer))]
    public class Weapon : MonoBehaviour
    {
        [Range(1, 5)]
        [SerializeField] private int _maxIterations = 3;
    
        [SerializeField] private float _maxDistance = 10f;
    
        public int _count;
        public LineRenderer _line;
    
        public Transform Firepoint;
        public GameObject BulletPrefab;
        public GameObject FirePrefab;
    
        private void Start()
        {
            _line = GetComponent<LineRenderer>();
        }
    
        // Update is called once per frame
        private void Update()
        {
            if (Input.GetButtonDown("Fire1"))
            {
                Shoot();
            }
    
            _count = 0;
            _line.SetVertexCount(1);
            _line.SetPosition(0, transform.position);
            _line.enabled = true;
            RayCast(transform.position, transform.up);
        }
    
        private void Shoot()
        {
            //shooting logic
            var destroyBullet = Instantiate(BulletPrefab, Firepoint.position, Firepoint.rotation);
            Destroy(destroyBullet, 10f);
            var destroyFire = Instantiate(FirePrefab, Firepoint.position, Firepoint.rotation);
            Destroy(destroyFire, 0.3f);
        }
    
        private bool RayCast(Vector2 position, Vector2 direction)
        {
            RaycastHit2D hit = Physics2D.Raycast(position, direction, _maxDistance);
            if (hit && _count <= _maxIterations - 1)
            {
                _count++;
                var reflectAngle = Vector2.Reflect(direction, hit.normal);
                _line.SetVertexCount(_count + 1);
                _line.SetPosition(_count, hit.point);
                RayCast(hit.point + reflectAngle, reflectAngle);
                return true;
            }
    
            if (hit == false)
            {
                _line.SetVertexCount(_count + 2);
                _line.SetPosition(_count + 1, position + direction * _maxDistance);
            }
            return false;
        }
    }
    

    【讨论】:

    • 感谢您的帮助。我回家后会测试并提供反馈。
    • 你好@Tedium Interactive。谢谢!!我测试了你的代码,现在我已经有了这条线,但我必须做一些调整。瞄准线不指向枪的方向,而且在我看来子弹的轨迹与瞄准线的轨迹不同,对吧?游戏动图:ibb.co/MDsMRM0
    • 是的,它看起来像@tigrev。第一行的方向由更新函数中的RayCast(transform.position, transform.up); 确定。您可以将其更改为RayCast(transform.position, transform.right);。但这取决于弹丸的初始方向。
    • 就是这样!我更改为 transform.right,现在瞄准线指向枪的方向。 ? 我现在的问题是子弹发射的轨迹与瞄准线不一样...游戏图片:ibb.co/4Y5vwGM
    【解决方案2】:

    好吧,我改变了子弹的物理材质,Friction 为 0,Bounciness 为 1。在刚体 2D 上,线性阻力、角度阻力和重力比例都为 0。虽然它不是完美的反弹,但它是非常接近我对游戏的打算。谢谢!看看吧:Game Gif

    唯一不利的是,我的子弹在反弹后没有朝着运动方向转动。我尝试使用transform.LookAt,但没有用。这是我的子弹脚本:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
    
     public class Bullet : MonoBehaviour
     {
     public float speed = 20f;
     public Rigidbody2D myRigidbody;
    
    
     // Start is called before the first frame update
     void Start()
     {
         this.myRigidbody = this.GetComponent<Rigidbody2D>();
         this.myRigidbody.velocity = transform.right * speed;
         transform.LookAt(transform.position + this.myRigidbody.velocity);
     }
     }
    

    但现在我遇到了这个错误:错误 CS0034 Operator '+' is ambiguous on type of 'Vector3' and 'Vector2'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      相关资源
      最近更新 更多