紅外線設置程式碼:
Raycast (位置 : Vector3, 方向 : Vector3, 長度 : float = Mathf.Infinity, 指定碰撞 : int = kDefaultRaycastLayers) : 回傳布林值
本次碰撞視圖:
碰撞程式碼:
using UnityEngine; using System.Collections; public class Shoot : MonoBehaviour { public Transform Gun; void Update () { /*Ray Shoot OutBounds*/ RaycastHit hit; Vector3 forward = transform.TransformDirection(Vector3.forward) * 10; Debug.DrawRay(Gun.position,Gun.rotation * Vector3.forward * 200,Color.white); if(Physics.Raycast(Gun.position,Gun.rotation * Vector3.forward * 200,out hit)){ if(Input.GetMouseButtonDown(0)){ print("目標: " + hit.collider.tag); Debug.DrawRay(Gun.position,Gun.rotation * Vector3.forward * 200,Color.green); } } }模擬只要碰到目標,按下開槍即取得資訊
參考:
Yenlin Huang Lab Ex: http://lolikitty.pixnet.net/blog/post/54979518-unity-%E5%B0%84%E7%B7%9A(ray)%E5%B0%8D%E7%89%A9%E9%AB%94%E5%B0%84%E6%93%8A%E5%8F%96%E5%BE%97-%E5%BA%A7%E6%A8%99
Unity 聖典 Raycast.collider : http://game.ceeger.com/Script/RaycastHit/RaycastHit.collider.html
Unity 聖典 DrawRay: http://game.ceeger.com/Script/Debug/Debug.DrawRay.html
Unity 聖典 Raycast: http://game.ceeger.com/Script/Physics/Physics.Raycast.html
Unity Official Api Ray: http://docs.unity3d.com/ScriptReference/Ray-ctor.html
沒有留言:
張貼留言