unity之ScreenPointToRay:屏幕位置转射线
2017-02-28 21:15
429 查看
using UnityEngine; using System.Collections; public class Weapon : MonoBehaviour { private Ray ray; private RaycastHit hit; private Transform tran; void Start () { tran = gameObject.GetComponent<Transform>(); } void Update () { ray=Camera.main.ScreenPointToRay(Input.mousePosition);//摄像机发射射线到屏幕点。 if (Physics.Raycast(ray, out hit))//射线发出并碰撞到外壳,那么手臂就应该朝向碰撞点 { tran.LookAt(hit.point); } } }
1: C#: Camera.main.ScreenPointToRay(Input.mousePosition); 此方法是Camera类下的一个:从摄像机发射射线到屏幕,并返回这条射线。
参数是屏幕中一个点,上例是鼠标在屏幕中点击的点,且由主摄像机发射射线。
返回的是一个射线类型 Ray ray;,可以用ray接收射线。ray带有射线的相关信息。
2:Physics.Raycast(ray,out hit)是射线检测的一个方法。当被碰撞到的物体含有conllider时, RaycastHit hit; hit就记录下被碰物体的相关信息,例如:上例中的hit.transform就是被碰物体的transform。例如hit.collider表示碰到的碰撞器,hit.distance表示射线原点到碰撞点之间的距离,是碰撞检测中的碰撞信息
hit.collider.tag=="FeiPan",表示被撞碰撞器的标签是FeiPan。
3: 返回布尔(bool)值,当射线和任何碰撞器相交时,返回true,否则为false。也就是说,当射线碰触到任何碰撞器时返回真,否则返回假。
参数有:(ray, out hit),ray是射线的起点和方向,hit记录射线和碰撞器碰撞的信息。
4:Debug.DrawRay(Vector3,Vector3,Color);此函数绘制射线 。第一个是起点位置,第二个是射线的方向
效果:挂载脚本的物体,指向鼠标在屏幕中的点
相关文章推荐
- ScreenPointToRay - 近视口到屏幕的射线
- Unity 人物如何跟随鼠标转向 ScreenPointToRay RaycastHit Raycast transform.LookAt
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition)会报错 空引用
- 得到屏幕坐标toScreenPoint
- Unity WorldToScreenPoint坐标变换
- Unity3D中ScreenPointToRay函数的分析-个人见解
- Unity3D中ScreenPointToRay方法使用解析
- ScreenPointToRay
- Unity ScreentoWorldPoint中的z坐标说明
- camera.ScreenPointToRay(),camera.ScreenToWorldPoint()与Raycast()
- android 中获取当前焦点所在屏幕中的位置 view.getLocationOnScreen(location)
- LocationOnScreen-控件在手机屏幕中的位置坐标
- 如何实现标准TCODE的屏幕增强(HOWTO:Implement a screen exit to a standard SAP transaction)
- 如何实现标准TCODE的屏幕增强(HOWTO:Implement a screen exit to a standard SAP transaction)
- ClientToScreen这个函数的功能:该函数将指定点,或者矩形的用户坐标转换成屏幕坐标。
- UIView获取屏幕Screen中的坐标位置
- Which is the fastest way to draw the screen in Symbian?---Symbian中最快速屏幕绘画的两种方法
- Camera.ScreenToWorldPoint
- [转]如何分屏幕(How to split the Screen)
- Control.PointToScreen 方法(转)