C# 行渲染器未按预期工作?
我使用线条渲染器创建水果忍者风格的轻扫效果。但它不起作用。这条线应该跟在鼠标的位置后面,但它没有这样做。这是代码。请帮我解决这个问题。这个脚本被附加到游戏对象线(空)的位置(0,0,1)C# 行渲染器未按预期工作?,c#,unity3d,C#,Unity3d,我使用线条渲染器创建水果忍者风格的轻扫效果。但它不起作用。这条线应该跟在鼠标的位置后面,但它没有这样做。这是代码。请帮我解决这个问题。这个脚本被附加到游戏对象线(空)的位置(0,0,1) 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共课路线:单一行为{ int vertexcount=0; bool-mousedown=false; 专线; 无效开始(){ line=GetComponent(); } //每帧调用一次更新 无效更新
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共课路线:单一行为{
int vertexcount=0;
bool-mousedown=false;
专线;
无效开始(){
line=GetComponent();
}
//每帧调用一次更新
无效更新(){
单击鼠标时调用if(Input.GetMouseButtonDown(0))//
{
mousedown=true;
}
如果(鼠标向下)
{
Debug.Log(“被调用”);
line.positionCount=vertexcount+1;
Vector3 mousepos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
行。设置位置(垂直计数、鼠标点);
vertexcount++;
}
释放鼠标时调用if(Input.GetMouseButtonUp(0))//
{
vertexcount=0;
line.positionCount=0;
mousedown=false;
}
}
}
为linerender启用世界空间坐标。在您的情况下,我认为最好使用
private TrailRenderer trail;
公众漂浮深度;
void Start()
{
深度=10;
trail=GetComponent();
}
无效更新()
{
if(输入。GetMouseButton(0))
{
Vector3 mousepos=新的Vector3(Input.mousePosition.x,Input.mousePosition.y,深度);
Vector3位置=Camera.main.ScreenToWorldPoint(鼠标点);
trail.transform.position=位置;
}
}
更改
TrailRenderer
组件上的Time
、MinVertexDistance
(和其他)属性以获得所需的效果。到底什么不起作用;我们说的是一个错误,还是不是期望的效果?@Jereon期望的效果。我告诉它要像水果忍者一样去检查直线上的点的矢量3.Z,如果它们能够被相机看到。@Jeroen我试过不工作linerender是否启用了使用世界空间,它的游戏对象是什么东西的孩子吗?这没有什么,然后还有一个额外的问题,因为这肯定是一个问题本身。您是否看到任何行?是的,当我手动更改行的位置时,我看到了行,但代码没有这样做
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;
}
}
}
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;
}
}