2017-08-06 66 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(及其他)屬性得到想要的效果。