Unity 2d游戏:如何更改瞄准线(线渲染器)点?

时间:2019-07-29 10:59:43

标签: c# unity3d 2d-games

我正在为一个带瞄准线的射击游戏开发一个小型游戏项目,我能够使用Line Renderer制作该瞄准线,但是我无法更改该线条的形状。我想要一些像小圆点的东西。在我看来,这与“检查器”选项卡中的“默认”粒子有关,但是如何更改这些内容?我会创建材料并替换它吗?如果是这样怎么办?

我在这里放置了一些图像,说明此时的瞄准线如何,“检查器”选项卡的状态以及我的“线条渲染器”代码。非常感谢您的帮助。

图片:https://imgur.com/Z9UYK3H

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

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

[SerializeField] private float _maxDistance = 25f;

public int _count;
public LineRenderer _line;
public int ammo;

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") && ammo > 0)
    {
        Shoot();
    }

    _count = 0;
    _line.SetVertexCount(1);
    _line.SetPosition(0, transform.position);
    _line.enabled = true;
    RayCast(transform.position, transform.right);
}

private void Shoot()
{
    //shooting logic
    var destroyBullet = Instantiate(BulletPrefab, Firepoint.position, Firepoint.rotation);
    ammo--;
    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 :(得分:0)

LineRenderer组件上,将Texture Mode更改为Tile,并确保将Corner Vertices设置为至少1

如果线条宽度不是1,请确保使用线条渲染器宽度的倒数来修改材质的Tiling X属性。

示例:线宽:0.25,材料x拼贴:1.0 / 0.25 = 4

enter image description here