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

查看:216
本文介绍了将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 Colider 2D 。如果您的Sprite是圆形的,还可以对Sprite使用 Circle Collider 2D 。如果未在Sprite中添加任何2D碰撞器,则ray在单击时不会检测到该对象。

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天全站免登陆