unity3d 求射线碰撞物体的例子,要求只可以与某层物体发生碰撞.

来源:学生作业帮助网 编辑:作业帮 时间:2024/07/09 03:58:23
unity3d 求射线碰撞物体的例子,要求只可以与某层物体发生碰撞.
xTMk@+Cdamw`-t?`$S0 A;>Owm]>yQ:q:`ݴ۾y-; 4ۆ ū 'p1%aTldd[=F-aG*0˙եX ֐P&@H1F @".Ө k0xoJ<NrF̩1}\?=T9 ":|13A[:4U&L g*Z5="AD. ~ XMdi\1jԧi(9"`<okt:u獷BšD{~8?#c*V

unity3d 求射线碰撞物体的例子,要求只可以与某层物体发生碰撞.
unity3d 求射线碰撞物体的例子,要求只可以与某层物体发生碰撞.

unity3d 求射线碰撞物体的例子,要求只可以与某层物体发生碰撞.
c#:
using UnityEngine;
using System.Collections;
public class Pathing :MonoBehaviour {
private int LayerGround;
private bool CastRays = true;
void Start () {
LayerGround = LayerMask.NameToLayer("Ground");
}
void Update () {
if (CastRays) {
Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
// Raycast
if (Physics.Raycast(ray,out hit,Mathf.Infinity)) {
if (hit.transform.gameObject.layer == LayerGround) {
Debug.Log("Ground");//这里和指定层碰撞
} else {
Debug.Log("Other Objects");
}
}
}
}
}
js:
private var LayerGround;
private var CastRays :boolean = true;
function Start () {
LayerGround = LayerMask.NameToLayer("Ground");
}
function Update () {
if (CastRays) {
var ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
var hit :RaycastHit;
// Raycast
if (Physics.Raycast(ray,hit,Mathf.Infinity)) {
if (hit.transform.gameObject.layer == LayerGround) {
Debug.Log("Ground");//这里和指定层碰撞
} else {
Debug.Log("Other Objects");
}
}
}
}