【问题标题】:Click and Hold OnMouseDown Unity3D单击并按住 OnMouseDown Unity3D
【发布时间】:2015-09-22 02:47:04
【问题描述】:

我希望我的 OnMouseDown 命令仅在玩家按下游戏中的对象时才起作用。此时,玩家可以按任意位置,计时器将开始计时。有没有办法让计时器仅在单击并按住对象时启动?为了给你一些背景信息,我希望一个对象在玩家点击并按住它时变为非活动状态。

到目前为止,这是我的代码:

function Update () 
{
    if (Input.GetMouseButton(0)) 
    {
        timer += Time.deltaTime;
    }
    if ( Input.GetMouseButtonUp(0))
    {
        timer = 0;
    }
    if(timer > 1)
    {
        gameObject.SetActive(false);    
    }
}

【问题讨论】:

标签: unity3d onmousedown


【解决方案1】:

解决方案: 为了做到这一点,我使用了 Raycast:

var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;   

 if (Input.GetMouseButton(0) && coll.Raycast(ray,hit,100))
 {
 timer += Time.deltaTime;
 }
 if ( Input.GetMouseButtonUp(0))
 {
    timer = 0;
 }

【讨论】:

    猜你喜欢
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    相关资源
    最近更新 更多