Unity - 瞄准线(线渲染器)未在 2D 射击游戏中显示

Unity - Aim Line (Line Renderer) not showing in 2D shooter

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

我现在想整合一条瞄准线,预测子弹的轨迹,包括物体上的弹跳(泡泡射手风格)。

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

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

我的目标是:

我的线渲染器组件定义:

我的武器脚本:

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

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

好吧,我更改了子弹的物理特性 material,将摩擦力设为 0,将弹力设为 1。同样在 rigidbody2D 上,线性阻力、angular 阻力和重力比例都设为 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 运算符“+”在 'Vector3' 和 'Vector2'

类型的操作数上不明确