将 3D Raycast 转换为 Raycast2D
Convert 3D Raycast to Raycast2D
我有下面这段代码可以控制3个玩家中的一个被触摸的玩家。我能够通过在 2D 精灵后面添加 3D 立方体的方式 "cheating" 来实现它,因为我的游戏应该是 2D 的,而我很难在 2D 中实现它。我真的很困惑如何在 2D 中做到这一点,因为我真的很困惑参数。
虽然我已经按照上面的方式实现了,但是我还是想用纯2D来实现。而且因为我在选择的玩家移动时遇到了这个问题,所以精灵移动得更快。
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
。参数不同,下面的代码应该做的 it.You 也可能需要根据您的游戏逻辑将 &&
更改为 ||
。
Select Sprite 然后从编辑器中添加 Box Colider 2D。如果您的 Sprite 是圆形的,您还可以为 Sprite 使用 Circle Collider 2D。如果您不向 Sprite 添加任何 2D collider,则单击时 ray 将不会检测到对象。
对于移动设备
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();
}
}
我有下面这段代码可以控制3个玩家中的一个被触摸的玩家。我能够通过在 2D 精灵后面添加 3D 立方体的方式 "cheating" 来实现它,因为我的游戏应该是 2D 的,而我很难在 2D 中实现它。我真的很困惑如何在 2D 中做到这一点,因为我真的很困惑参数。
虽然我已经按照上面的方式实现了,但是我还是想用纯2D来实现。而且因为我在选择的玩家移动时遇到了这个问题,所以精灵移动得更快。
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
。参数不同,下面的代码应该做的 it.You 也可能需要根据您的游戏逻辑将 &&
更改为 ||
。
Select Sprite 然后从编辑器中添加 Box Colider 2D。如果您的 Sprite 是圆形的,您还可以为 Sprite 使用 Circle Collider 2D。如果您不向 Sprite 添加任何 2D collider,则单击时 ray 将不会检测到对象。
对于移动设备
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();
}
}