2017-08-06 64 views
0

我正在使用线渲染器来创建水果忍者风格刷卡效果。但它不工作。线应该按照鼠标pos,但它不这样做这是代码。请帮我解决这个问题。这个脚本在(0,0,1)的位置附加到gameobject行(空)。线渲染器不按预期工作?

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

什么是不工作;我们是在谈论一个错误,还是不是想要的效果? –

+0

@Jereon想要的效果。我告诉它是去水果忍者 –

+0

检查该行的点vector3.Z如果他们能够被相机查看。 –

回答

1

为linerender启用世界空间坐标。

+0

那什么都不做 –

+0

然后还有一个额外的问题,因为这肯定是一个问题本身。你是否看到任何线条? –

+0

是的,当我手动改变它的位置时,我看到了这一行,但代码没有这样做 –

0

就你而言,我认为最好使用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(及其他)属性得到想要的效果。