我正在使用线条渲染器来创建水果忍者风格的滑动效果。但是它没有用.Line应该跟随鼠标位置,但它没有这样做。这是代码。请帮我解决这个问题。并且此脚本附加到位于(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;
}
}
}
答案 0 :(得分:1)
为linerender启用世界空间坐标。
答案 1 :(得分: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;
}
}
更改Time
组件上的MinVertexDistance
,TrailRenderer
(和其他)属性,以获得所需的效果。