纳金网

标题: OnMouseDown 触发事件的问题? [打印本页]

作者: 她。    时间: 2013-5-13 08:46
标题: OnMouseDown 触发事件的问题?
我在测试一个简单的效果。点击cube-b 则 cube-a 慢慢移动到cube-b的位置上。
我用了这样的脚本
var speed = 0.001;
var obj: GameObject;
var target : Transform;
function OnMouseDown ()
{
obj = GameObject.Find("Cube_a");
obj.transform.position = Vector3.Lerp(obj.transform.position, target.position, Time.time * speed);
}
结果是鼠标点一下cube-a 动一下。不是连续的移动。
求解决方法。


作者: humazi    时间: 2013-5-13 09:55
OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider. 意思是说,当用户按下(按下,弹起这一过程)了鼠标键,则调用一次。

而OnMouseOver       

OnMouseOver is called every frame while the mouse is over the GUIElement or Collider.
对比这个,如果是每帧都调用,实际上帮助文档里面会指示出来的。


作者: humazi    时间: 2013-5-13 09:58
想修改,可以function Update(){
   if(Input.GetMouseButton(0)){ transform.position = Vector3.Lerp(obj.transform.position, target.position, Time.time * speed); }
}

类似这样
作者: 崇尚现在    时间: 2013-5-13 16:53
这样写就能满足你的要求了

using UnityEngine;
using System.Collections;

public class MoveTest : MonoBehaviour
{
    public Vector3 margin;
        // Use this for initialization
        void Start () {
       
        }
       
        // Update is called once per frame
        void Update () {

        transform.position = Vector3.Lerp(transform.position, margin, Time.deltaTime * 1f);     
        }

    void OnGUI()
    {
     
        if (GUILayout.Button("开始运动"))
        {
            margin+=new Vector3 (5,0,0);     
        }
    }
}

作者: 齐天大圣    时间: 2013-5-16 21:17
我也是鸣人··
作者: fuland539    时间: 2013-5-18 23:13
要repeatebuttong吧




欢迎光临 纳金网 (http://c-www.narkii.com/club/) Powered by Discuz! X2.5