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

时间:2019-02-06 19:24:13

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

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

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

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

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

我的目标是:

Game idea

我的Line Renderer组件定义:

Line Renderer Definitions

我的武器脚本:

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

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

好吧,我将子弹的物理材料更改为“摩擦力”(Friction)为0,将“弹跳度”(Bounciness)更改为1。此外,在“ bodybody2D”上,线性阻力,角阻力和重力比例全部为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'的操作数上不明确