这是我的游戏的脚本。但是它只适用于像cube这样的3d对象,而不适用于游戏中的2D图像。如何修复它?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class touchinput : MonoBehaviour {
void Update () {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay( Input.GetTouch(0).position );
RaycastHit hit;
if ( Physics.Raycast(Ray, out hit))
{
Destroy(hit.collider.gameObject);
}
}
}
}
我尝试更改为此但我收到很多错误,并且不知道如何修复。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class touchinput : MonoBehaviour {
void Update () {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
{
Ray2D ray = Camera.main.ScreenPointToRay( Input.GetTouch(0).position );
RaycastHit2D hit;
if ( Physics2D.Raycast(Ray2D, out hit))
{
Destroy(hit.collider.gameObject);
}
}
}
}
答案 0 :(得分:2)
Raycast确实不会对2D碰撞器起作用 前几天我找到了this方法,你可以尝试一下:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(wp.x, wp.y);
if (collider2D == Physics2D.OverlapPoint(touchPos))
{
//your code
}
}