将 3D Raycast 转换为 Raycast2D [英] Convert 3D Raycast to Raycast2D

查看:31
本文介绍了将 3D Raycast 转换为 Raycast2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有这个代码,它可以控制 3 个玩家中的一个被触摸的玩家.我能够通过在 2D 精灵后面添加 3D 立方体的方式来实现它,因为我的游戏应该是 2D 的,而我很难在 2D 中实现它.我真的很困惑如何在 2D 中做到这一点,因为我真的很困惑参数.

I have this code below which could control a touched player among 3 players. I was able to implement it by like sort of "cheating" by way of adding a 3D cube behind a 2D sprite since my game should be in 2D and I am having hard time implementing it in 2D. I'm really confused on how to do it in 2D because I'm really confuse on the parameters.

虽然我已经通过上面提到的方式实现了它,但我仍然想在纯2D中实现它.而且因为我在选择的玩家移动时有这个问题,所以精灵移动得更快.

Although I already implemented it by the way mentioned above, I still want to implement it in pure 2D. And because I have this problem when the selected player moves, the sprite moves faster.

public GameObject target = null;

// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () 
{
   if(Input.touchCount > 0 || Input.GetTouch(0).phase == TouchPhase.Began)
   {
       Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
       Debug.DrawRay(ray.origin,ray.direction * 20,Color.red);
       RaycastHit hit;
       if(Physics.Raycast(ray, out hit,Mathf.Infinity))
       {
            Debug.Log(hit.transform.gameObject.name);
            if(this.target != null){
                SelectMove sm = this.target.GetComponent<SelectMove>();
                if(sm != null){ sm.enabled = false; }
            }
            target = hit.transform.gameObject; 
            //Destroy(hit.transform.gameObject);
            selectedPlayer();
        }
    }
}

void selectedPlayer(){
    SelectMove sm = this.target.GetComponent<SelectMove>();
    if(sm == null){
        target.AddComponent<SelectMove>();
    }
    sm.enabled = true;  
}

推荐答案

使用 RaycastHit2D 来代替 RaycastHit 然后改变 Physics.Raycast> 到 Physics2D.Raycast.参数不同,下面的代码应该这样做.您可能还需要根据您的游戏逻辑将 && 更改为 ||.

use RaycastHit2D to do this instead of RaycastHit then change Physics.Raycast to Physics2D.Raycast. The parameters are different the code below should do it.You may also have to change && to || depending on your game logic.

选择Sprite,然后从编辑器中添加Box Collider 2D.如果您的 Sprite 是圆形的,您还可以将 Circle Collider 2D 用于 Sprite.如果您不向 Sprite 添加任何 2D 碰撞器,则单击时光线将不会检测到该对象.

Select the Sprite then add Box Colider 2D from the Editor. You can also use Circle Collider 2D for the Sprite if your Sprite is round. If you don't add any 2D collider to the Sprite, ray won't detect the object when clicked on.

对于移动设备

if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Began))
  {
            Vector2 cubeRay = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            RaycastHit2D cubeHit = Physics2D.Raycast(cubeRay, Vector2.zero);
            if (cubeHit)
            {
                //We hit something
                Debug.Log(cubeHit.transform.gameObject.name);
                if (this.target != null)
                {
                    SelectMove sm = this.target.GetComponent<SelectMove>();
                    if (sm != null) { sm.enabled = false; }
                }
                target = cubeHit.transform.gameObject;
                //Destroy(cubeHit.transform.gameObject);
                selectedPlayer();
            }
  }

对于桌面/编辑器

if (Input.GetMouseButtonDown(0))
        {
            Vector2 cubeRay = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D cubeHit = Physics2D.Raycast(cubeRay, Vector2.zero);
            if (cubeHit)
            {
                //We hit something
                Debug.Log(cubeHit.transform.gameObject.name);
                if (this.target != null)
                {
                    SelectMove sm = this.target.GetComponent<SelectMove>();
                    if (sm != null) { sm.enabled = false; }
                }
                target = cubeHit.transform.gameObject;
                //Destroy(cubeHit.transform.gameObject);
                selectedPlayer();
            }
        }

这篇关于将 3D Raycast 转换为 Raycast2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆