您好,登錄后才能下訂單哦!
keep study very day!寫寫今天的學習收獲,今天學習到了平面坐標跟世界坐標的相互轉換。
using UnityEngine; using System.Collections; public class cameramove : MonoBehaviour { private Vector3 world; private float speed = 0;//物體移動的速度 // Use this for initialization void Start() { world.x = -2; world.y = 0; world.z = 6; } // Update is called once per frame void Update() { Vector2 screenpos = Camera.main.WorldToScreenPoint(transform.position);//物體的世界坐標轉化成屏幕坐標 Vector3 e = Input.mousePosition;//鼠標的位置 //當點擊鼠標中鍵時 if (Input.GetMouseButtonDown(2)) { //e.z=screenpos.z;//1.因為鼠標的屏幕 Z 坐標的默認值是0,所以需要一個z坐標 //e.z=1;//將鼠標 //攝像機要垂直于x-z平面 //world=Camera.main.ScreenToWorldPoint(e); world = new Vector3(Input.mousePosition.x/Screen.width,Input.mousePosition.y/Screen.height,106f ); Vector3 world1= Camera.main.ViewportToWorldPoint(new Vector3(world.x, world.y, 106f)); //world.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x; //world.z = Camera.main.ScreenToWorldPoint(Input.mousePosition).y; //world.y = transform.position.y; print("new x:" + world.x); print("new y:" + world.y); print("new z:" + world.z); //創建物體 //GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Sphere); //goNew.transform.position = world1; transform.LookAt(world1); } } }
==================== 迂者 丁小未 CSDN博客專欄=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ群:858550 cocos2dx QQ群:280818155
====================== 相互學習,共同進步 ===================
轉載請注明出處:http://blog.csdn.net/dingxiaowei2013/article/details/14498293
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。