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

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 11:39:13

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");
}
}
}
}