using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))//0 1 2 分别代表鼠标的左右和中间按键
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 主相机屏幕点转换为朝向鼠标点击处的射线
RaycastHit hitinfo;//用来接受射线的信息
if (Physics.Raycast(ray,out hitinfo))//射线碰到了物体
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
{//触摸次数为1,一个触摸点,手指刚触摸到屏幕的时候
if (Input.GetTouch(0).tapCount==2)//在触摸点0处的敲击次数=2,意味着双击
Destroy(hitinfo.collider.gameObject);//双击摧毁事物
}
}
}
}
}
直接在update里面写就行, // Update is called once per frame,每一帧调用一次
//
// 摘要:
// Returns true during the frame the user pressed the given mouse button.
//
// 参数:
// button:
[NativeThrows]
public static bool GetMouseButtonDown(int button);
在用户释放鼠标按钮并再次按下它之前,它不会返回 true。 按钮值为 0 表示主按钮(通常是左按钮),1 表示辅助按钮,2 表示中间按钮。
Physics.Raycast(ray,out hitinfo)
//ray 光线的起点和方向。
//hitInfo 如果返回 true,则 hitInfo 将包含有关最近的碰撞体的命中位置的更多信息。
//bool 当光线与任何碰撞体相交时,返回 true,否则返回 false。
touchCount为触摸次数,也意味着不同的触摸对象
//
// 摘要:
// Number of touches. Guaranteed not to change throughout the frame. (Read Only)
public static int touchCount { get; }
GetTouch(0).phase,获得第一个触摸点的状态
index的真正作用,获取触摸到屏幕的第几个点。要知道同一时刻可能有多个点在被触碰,所以如果当前你只有一个手指在按屏幕,那当然就只有(0)了,如果有两个,那0是一个点,1是一个点。以此类推。
gettouch()并不代表你当前时刻一定有手指在触摸屏幕,它只是从touches列表里获取,如果列表为空,自然报错了。而那个touches列表在哪呢?Input.touches,是有这个方法的,所以其实gettouch(0)和Touch[] touches = Input.touches; touches[0] 的效果是一样的。
hitinfo.collider
射线撞击,因此撞击的物体也要添加collider组件