[英]Mouse trail is teleporting when i click
im trying to make a mouse trail. 我试图做一个鼠标踪迹。 So when you hold the mouse button down, it starts the trail and you can move around and then when you release, it dissapers.
因此,当您按住鼠标按钮时,它会启动轨迹,您可以四处移动,然后当您释放时,它会消失。 Im using the trail renderer for this.
我正在使用线索渲染器。
Im tryna to replica the blade thats seen in stuff like fruit ninja. 我试着复制像水果忍者这样的东西中看到的刀片。 So i have an empty game object called blade with a kinematic rigidbody 2d and my blade script.
所以我有一个名为blade的空游戏对象,带有运动刚体2d和我的刀片脚本。
I then have a trail which is a prefab that i drag into the blade script. 然后,我有一个跟踪,这是一个预制,我拖入刀片脚本。 Here is the blade script:
这是刀片脚本:
bool isCutting = false;
Rigidbody2D rb;
Camera cam;
public GameObject trail;
GameObject currentTrail;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
cam = Camera.main;
}
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
isCutting = true;
currentTrail = Instantiate(trail, transform);
} else if (Input.GetMouseButtonUp(0))
{
isCutting = false;
currentTrail.transform.SetParent(null);
}
if (isCutting)
{
rb.position = cam.ScreenToWorldPoint(Input.mousePosition);
Destroy(currentTrail, 2f);
}
}
The only problem is, when i hold my mouse down, the blade insanely teleports to my mouse position with the trail renderer. 唯一的问题是,当我按住鼠标时,刀片疯狂地传送到我的鼠标位置与跟踪渲染器。
So i think the default blade position is in the middle of the camera, if i drag and hold at the top, u can see the beginning of a blade is a straight line trail from the middle to the top and i want the trail to start where i click. 所以我认为默认的刀片位置在相机的中间,如果我拖动并保持在顶部,你可以看到刀片的开始是从中间到顶部的直线轨迹,我想要开始跟踪我点击的地方。
I hope that makes sense. 我希望这是有道理的。 Thanks
谢谢
Try changing the last section of the Update()
method to: 尝试将
Update()
方法的最后一部分更改为:
if (isCutting)
{
currentTrail.transform.position = rb.position = cam.ScreenToWorldPoint(Input.mousePosition);
Destroy(currentTrail, 2f);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.