简体   繁体   English

当我点击时鼠标轨迹传送

[英]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.

相关问题 传送时轨迹渲染器会留下轨迹 - Trail renderer is leaving a trail when teleporting 当我尝试执行鼠标单击时,SendInput移动光标 - SendInput moves the cursor when I try to perform a mouse click 单击pictureBox时,如何将鼠标光标坐标转换为屏幕相对鼠标光标坐标? - How can I convert the mouse cursor coordinates when click on pictureBox are to the screen relative mouse cursor coordinates? 如何同时使用两个脚本航点和传送以及如何使传送更好地工作? - How can I work with both scripts waypoints and teleporting together and how to make the teleporting to work better? 完成后销毁路径 - Destroy trail when finished 单击并将其添加到队列时,如何在鼠标位置绘制事物? - How do I draw a thing on the mouse location when I click and add it to a queue? 我有一个pictureBox1_MouseDown事件,当我单击鼠标左键时,它的效果很好 - I have a pictureBox1_MouseDown event and when i click the mouse left button it dosent work good 使用鼠标双击时计时器出现问题 - Problem with the Timer when using mouse double click 当鼠标在某个区域中一段时间​​后单击 - Click when mouse is in an area for some time 为什么在将文本菜单添加到richtextbox时,我需要两次单击鼠标右键才能查看菜单? - Why when adding a contextmenu to richtextbox i need to make twice right mouse button click to see the menus?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM