Unity3d 使用Raycast2D检测对象
我正在研究简单的策略游戏机制。我有一个兵营预制房。当我在场景中添加营房并单击营房时,我收到一个Unity3d 使用Raycast2D检测对象,unity3d,raycasting,Unity3d,Raycasting,我正在研究简单的策略游戏机制。我有一个兵营预制房。当我在场景中添加营房并单击营房时,我收到一个NullReferenceException错误: NullReferenceException:对象引用未设置为对象PlacementController.Update()的实例(位于Assets/Scripts/PlacementController.cs:64) 当我尝试使用Raycast2D访问营房的碰撞器名称时收到错误 营房预制有一个盒子碰撞R2D碰撞器(触发器已选中),其标签为“建筑”,其图
NullReferenceException
错误:
NullReferenceException:对象引用未设置为对象PlacementController.Update()的实例(位于Assets/Scripts/PlacementController.cs:64)
当我尝试使用Raycast2D访问营房的碰撞器名称时收到错误
营房预制有一个盒子碰撞R2D碰撞器(触发器已选中),其标签为“建筑”,其图层为“建筑”。它有一个刚体2D组件,是一个运动学刚体
我想不出这个问题。请帮帮我
谢谢你抽出时间
using UnityEngine;
using System.Collections;
public class PlacementController : MonoBehaviour
{
private Buildings buildings;
private Transform currentBuilding;
private bool _hasPlaced;
public LayerMask BuildingsMask;
public void SelectBuilding(GameObject g)
{
_hasPlaced = false;
currentBuilding = ((GameObject)Instantiate(g)).transform;
buildings = currentBuilding.GetComponent<Buildings>();
}
bool CheckPosition()
{
if (buildings.CollidersList.Count > 0)
{
return false;
}
return true;
}
// Update is called once per frame
void Update () {
Vector3 m = Input.mousePosition;
m = new Vector3(m.x, m.y, transform.position.z);
Vector3 p = GetComponent<Camera>().ScreenToWorldPoint(m);
if (currentBuilding != null && !_hasPlaced)
{
currentBuilding.position = new Vector3(p.x,p.y,0);
if (Input.GetMouseButtonDown(0))
{
if (CheckPosition())
{
_hasPlaced = true;
}
}
}
else
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = new RaycastHit2D();
Ray2D ray2D = new Ray2D(new Vector2(p.x,p.y), Vector3.down );
//Ray2D ray = new Ray(transform.position,new Vector3(p.x,p.y,p.z));
if (Physics2D.Raycast(new Vector2(p.x,p.y),Vector3.down,5.0f,BuildingsMask) )
{
Debug.Log(hit.collider.name); //error
}
}
}
}
Unity有两个物理引擎,它们非常相似,但这是一个以微妙和令人困惑的方式不同的领域 3D引擎提供,命中时返回
true
,否则返回false
,如果需要了解更多命中信息,可以通过引用传递RaycastHit
2D引擎提供,它在命中时返回RaycastHit2D
,否则返回null
。按照编写代码的方式,您访问的命中
与raycast调用返回的命中不同
因此,您需要更接近这一点:
RaycastHit2D hit = Physics2D.Raycast(...); //edit in your raycast settings
if (hit) {
//do something with the hit data
}
(您可能会注意到,RaycastHit2D
隐式转换为bool
)
Unity在很长一段时间内只拥有3D引擎,所以很多旧文档都会说这是唯一的一个。注意这一点。在新的UI系统中,您不再需要像这样手动处理点击。只需在你的MonoBehavior上实现,并确保场景中有和存在。好的!我查看了所有的互联网,没有人了解人们真正需要什么。当谈论raycast2D时,我终于找到了他们需要的东西,拿走了,并感到高兴。)我会尝试在任何地方发布答案,这样人们就可以很容易地找到它,如果它需要的话。 从相机屏幕到2D精灵,精灵应该与任何碰撞器,刚体上的精灵不需要
//create 2 empty places for objects
public RaycastHit2D hit;
public GameObject choosen;
//in update put click on mouse //take Method play by clicking mouse
void Update(){
if (Input.GetKeyDown (KeyCode.Mouse0)) {
RayCaster ();
}
}
// create raycast Method
void RayCaster (){
RaycastHit2D ray = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay (Input.mousePosition));//making ray and object, taking mouse position on screen from camera
if(!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject (-1)) {
if (ray.collider != null) {// for non error, check maybe you hit none collider sprite
hit = ray;// not hit is our obj from scene, but we cant work with it as an object
choosen = hit.transform.gameObject;// making hit obj as normal object, now we can work with it like with 3Dobject, not as sprite
}
}
}
然后与choosen obj合作
把脚本放到相机上,现在每个人都会很高兴,因为在互联网上甚至unity3D社区都不了解人们对raycast2D的真正需求,希望将来他们能让这个功能变得更简单。)谢谢@ааааааааааааа,我只是需要用鼠标位置击中一条射线,然后知道这个位置是否与一些2D游戏对象(如精灵)击中 我正在调用这个命中OnMouseUp()方法
//create 2 empty places for objects
public RaycastHit2D hit;
public GameObject choosen;
//in update put click on mouse //take Method play by clicking mouse
void Update(){
if (Input.GetKeyDown (KeyCode.Mouse0)) {
RayCaster ();
}
}
// create raycast Method
void RayCaster (){
RaycastHit2D ray = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay (Input.mousePosition));//making ray and object, taking mouse position on screen from camera
if(!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject (-1)) {
if (ray.collider != null) {// for non error, check maybe you hit none collider sprite
hit = ray;// not hit is our obj from scene, but we cant work with it as an object
choosen = hit.transform.gameObject;// making hit obj as normal object, now we can work with it like with 3Dobject, not as sprite
}
}
}
void OnMouseUp(){
RaycastHit2D ray = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));
if (ray)
{
GameObject hittedGameObject = ray.collider.gameObject;
// Do something else
}
}