纳金网

标题: 屏幕坐标跟世界坐标的转换 [打印本页]

作者: 狂风大尉    时间: 2014-6-24 17:20
标题: 屏幕坐标跟世界坐标的转换
  1.  using UnityEngine;

  2.   using System.Collections;

  3.   public class cameramove : MonoBehaviour

  4.   {

  5.   private Vector3 world;

  6.   private float speed = 0;//物体移动的速度

  7.   // Use this for initialization

  8.   void Start()

  9.   {

  10.   world.x = -2;

  11.   world.y = 0;

  12.   world.z = 6;

  13.   }

  14.   // Update is called once per frame

  15.   void Update()

  16.   {

  17.   Vector2 screenpos = Camera.main.WorldToScreenPoint(transform.position);//物体的世界坐标转化成屏幕坐标

  18.   Vector3 e = Input.mousePosition;//鼠标的位置

  19.   //当点击鼠标中键时

  20.   //unity3d论坛http://www.unitymanual.com

  21.   if (Input.GetMouseButtonDown(2))

  22.   {

  23.   //e.z=screenpos.z;//1.因为鼠标的屏幕 Z 坐标的默认值是0,所以需要一个z坐标

  24.   //e.z=1;//将鼠标

  25.   //摄像机要垂直于x-z平面

  26.   //world=Camera.main.ScreenToWorldPoint(e);

  27.   world = new Vector3(Input.mousePosition.x/Screen.width,Input.mousePosition.y/Screen.height,106f );

  28.   Vector3 world1= Camera.main.ViewportToWorldPoint(new Vector3(world.x, world.y, 106f));

  29.   //world.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;

  30.   //world.z = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;

  31.   //world.y = transform.position.y;

  32.   print("new x:" + world.x);

  33.   print("new y:" + world.y);

  34.   print("new z:" + world.z);

  35.   //创建物体

  36.   //GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Sphere);

  37.   //goNew.transform.position = world1;

  38.   transform.LookAt(world1);

  39.   }

  40.   }

  41.   }
复制代码

作者: hyui    时间: 2014-6-24 18:02
Thanks for this!
作者: hariboot    时间: 2014-6-25 09:15

MARK THIS CONTENT
作者: hxsdsjr    时间: 2014-7-30 15:04
学习了




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