纳金网

标题: unity 中如何获取鼠标的移动速度? [打印本页]

作者: 她。    时间: 2013-4-10 09:46
标题: unity 中如何获取鼠标的移动速度?
c#的代码 想获取鼠标的move_speed 怎么获取?


作者: wheroy    时间: 2016-5-5 08:50
单位时间内获取鼠标的两个点,算出距离,除以时间就是鼠标移动速度
作者: wheroy    时间: 2016-5-5 09:07
public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    string mouseV;
    Vector3? p1;
   
   
        void Start () {
       
        }
       
        // Update is called once per frame
        void Update () {
      
        var p2 = Input.mousePosition;
        float t12 = Time.deltaTime;
        if (p1 != null)
        {
            float d = Vector3.Distance(p1.Value , p2);
            mouseV = (d / t12).ToString ();
        }
        p1 = p2;

        }
    void OnGUI()
    {
        if (mouseV == null) return;
        GUILayout.Label(mouseV);
    }
}
作者: wheroy    时间: 2016-5-5 09:09
将以上脚本附在一个gameobject上即可
作者: wheroy    时间: 2016-5-6 11:40
真是石沉大海了
作者: loyouliuzy    时间: 2016-8-8 14:18
wheroy 发表于 2016-5-6 11:40
真是石沉大海了

谢谢分享
作者: 635132254    时间: 2016-11-10 16:15
不错不错




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