团结 - 瞄准线(行渲染器)的2D射击游戏不显示

问题描述 投票:1回答:2

我是一个新手团结,我在一所学校的项目创建一个小的2D射击游戏,我已经成功地在即使反弹使用Unity物理学的某些对象主要角色的功能拍摄系统。

我现在想集成的目标线,将预测子弹的弹道轨迹,包括物体反弹(泡沫射手风格)。

我发现,使用光线投射和行渲染器的脚本和它理应做到这一点,我试图把它融入我的枪脚本,但虽然它不给任何错误,它根本就不当我测试游戏显示任何东西。我不知道问题出在我放在行渲染器组件的设置,或者是在脚本。

有人能帮助我了解在我的错误,并指出正确的方法是什么?

我的目标是:

Game idea

我行渲染器组件定义:

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;
}
}
unity3d 2d game-physics 2d-games raycasting
2个回答
1
投票

团结有两个物理引擎一个二维和一个三维。您提供的脚本依赖于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;
    }
}

0
投票

好吧,我改变了子弹的物理材料,摩擦为0和反弹力,以1上的rigidbody2D线性拖,拖角和重力规模都为0。虽然这不是一个完美的反弹,这是非常接近本来我是打算进行游戏。谢谢!检查出来:Game Gif

唯一的负面的事情是,我的子弹没有在反弹后运动的方向转动。我试图qazxsw POI使用,但没有奏效。这是我的子弹脚本:

transform.LookAt

但现在我有这个错误:错误CS0034运算符“+”是若即若离类型的操作数“的Vector3”和“Vector2”

© www.soinside.com 2019 - 2024. All rights reserved.