using UnityEngine;
using System.Collections;
public class ClickUIManager : MonoBehaviour {
private float OldTime =0.0f;
private float NewTime =0.0f;
private Vector3 mPosition;
public GameObject mClickTransfrom;
private float myScreenWidth;
private float myScreenHeight;
private float x =0.0f;
private float y = 0.0f;
private float z=10.0f;
void Start()
{
myScreenWidth = Screen.width;
myScreenHeight = Screen.height;
}
void FixedUpdate () {
if (Input.GetMouseButtonDown (0)) { //首先判斷是否點擊了鼠標左鍵
OldTime = Time.realtimeSinceStartup;
//定義一條射線,這條射線從攝像機屏幕射向鼠標所在位置
}
if(Input.GetMouseButtonUp(0))
{
NewTime = Time.realtimeSinceStartup;
if(NewTime - OldTime <0.15f)
{
x = Input.mousePosition.x - myScreenWidth/2;
y= Input.mousePosition.y - myScreenHeight/2;
mClickTransfrom.transform.localPosition =new Vector3(x/myScreenWidth*1080,y/myScreenHeight*1920,-500f);
mClickTransfrom.SetActive(false);
mClickTransfrom.SetActive(true);
}
}
}
}
這裏 通過判斷時間 來減少頻率, Input.mousePosition 是2維屏幕的直接座標,但是卻 以中心點爲0,0 根據實際屏幕大小得到的座標值,所以要對其進行處理後才能正常使用。鼠標點哪裏,哪裏出特效
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.