unity3d 求射线碰撞物体的例子,要求只可以与某层物体发生碰撞。
1个回答
展开全部
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");
}
}
}
}
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");
}
}
}
}
追答
那就判断从Cube发出的射线是否和你指定层(比如,程序中的Ground层)发生碰撞,如果碰撞的物体是Ground层的,就从Cube的postion画条线到hit的position,其他的不管
Debug.Log("Ground");
下加入
Debug.DrawLine(transform.position,hit.transform.position);
只要鼠标移动到是Ground层的物体就会从Cube发出射线到鼠标点,脚本是赋给Cube的
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
VSH艾羽
2024-10-27 广告
2024-10-27 广告
ANSYS是一款功能强大的有限元分析软件,广泛应用于航空航天、汽车、电子、能源等多个领域。它能够进行结构、流体、热、电磁等多物理场的仿真分析,支持复杂的几何建模、高效的网格划分和多种求解器,帮助工程师优化产品设计,减少开发成本,提高生产效率...
点击进入详情页
本回答由VSH艾羽提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询