【问题标题】:Line renderer not working as expected?线渲染器没有按预期工作?
【发布时间】:2017-08-06 13:05:50
【问题描述】:

我正在使用线条渲染器来创建水果忍者风格的滑动效果。但它不起作用。Line 应该跟随鼠标 pos 但它没有这样做 这是代码。请帮我解决这个问题。并且此脚本附加到 (0,0,1) 位置的游戏对象行(空)。

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

public class LineFOllow : MonoBehaviour {
    int vertexcount =0;
    bool mousedown = false;
    private LineRenderer line;

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

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0)) // gets called when mouse is clicked
        {
            mousedown = true;
        }

        if (mousedown)
        { 
            Debug.Log("called");
            line.positionCount = vertexcount+1;
            Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            line.SetPosition(vertexcount, mousepos);
            vertexcount++;
        }
        if (Input.GetMouseButtonUp(0))// gets called when mouse is released
        {
            vertexcount = 0;
            line.positionCount = 0;
            mousedown = false;
        }
    }
}

【问题讨论】:

  • 究竟是什么不起作用;我们是在谈论一个错误,还是不是想要的效果?
  • @Jereon 想要的效果。我告诉过它像水果忍者一样去
  • 检查线中点的vector3.Z是否可以被相机看到。
  • @Jeroen 我试过不行
  • 衬里渲染器是否启用了使用世界空间,它是游戏对象的子对象吗?

标签: c# unity3d


【解决方案1】:

为 linerender 启用世界空间坐标。

【讨论】:

  • 什么都不做
  • 那么还有一个额外的问题,因为这本身就是一个问题。你有没有看到任何线条?
  • 是的,当我手动更改它的位置时我看到了这条线,但代码没有这样做
【解决方案2】:

在你的情况下,我认为最好使用TrailRenderer

private TrailRenderer trail;
public float depth;
void Start()
{
    depth = 10;
    trail = GetComponent<TrailRenderer>();
}       
void Update()
{
    if (Input.GetMouseButton(0))
    {                      
        Vector3 mousepos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, depth);
        Vector3 position = Camera.main.ScreenToWorldPoint(mousepos);
        trail.transform.position = position;
    }
}

更改TrailRenderer 组件上的TimeMinVertexDistance(和其他)属性以获得所需的效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多