您的位置:首页 > 移动开发 > Unity3D

unity中让一个精灵在屏幕上跟随鼠标

2016-09-01 18:37 471 查看
using UnityEngine;
using System.Collections;

//把脚本挂到要跟随鼠标的精灵上
public class follw : MonoBehaviour {
    public Camera uiCamera;
    private Vector3 pos;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
        pos = Input.mousePosition;
        pos.x = Math
4000
f.Clamp01(pos.x / Screen.width);//固定鼠标的位置,0~1之间的数
        pos.y = Mathf.Clamp01(pos.y / Screen.height);
        //将得到的视口位置再转化为世界坐标。
        transform.position = uiCamera.ViewportToWorldPoint(pos);
    }

}

//跳到 Clamp01 里是这样的

using System;
public static float Clamp01 (float value)
{
    if (value < 0f)
    {
        return 0f;
    }
    if (value > 1f)
    {
        return 1f;
    }
    return value;
}

其实NGUI中自带的一个UICursor脚本,给要跟随的精灵添加上,吧UICamera拖上去就好了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: