当前位置:网站首页>[Unity] 交互之双击
[Unity] 交互之双击
2022-07-17 04:23:00 【Goodbye!】
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组件
边栏推荐
- C # explain out output parameters in detail
- Analysis of network attack detection technology for NDN
- V4l2 learning materials collection
- Sg90 actuator experiment based on STM32, including code (HAL Library)
- B+ tree stored procedures, triggers, substring and substr, and truncate and delete
- ASP. Net1==visual studio create asp net demo
- Wechat e-book reading applet graduation design of applet completion works (3) background function
- 删除文件 unable to find or create trash dirctory
- 如何配置Binlog
- MySql 一行变多行(根据特定符号分割)
猜你喜欢

TiDB学习笔记【初阶】

MAUI 框架入門學習05 MVVM數據模型理解

ASP. Net1==visual studio create asp net demo

Wechat e-book reading of small program graduation design (5) task book

PyTorch Image Models(timm)库

51单片机一究到底输入模式

Leetcode7 DFS + dynamic programming + double pointer

Distributed notes (02) - redis of distributed cache (brief description of uses, features, high availability solutions redis cluster, tweetproxy, CODIS)

priority_ Introduction and use of queue
![[Huang ah code] Introduction to MySQL - 5. Database tips: a single column group by will, and multiple columns?](/img/4d/552c4251779705a6dfd4ceb59c4261.png)
[Huang ah code] Introduction to MySQL - 5. Database tips: a single column group by will, and multiple columns?
随机推荐
JS modal box
Machine learning 09: unsupervised learning
实时BI(四)低成本的数据准实时处理思路
微信附近的人小程序怎么开(开通附近小程序的方法)
donet framework4. X==windows form application new project, through system Data. SqlClient connects to sqlserver to query
使用__slots__和__dict__来节省空间(简直就是质的飞越,LeetCode亲测有效)
Oracle操作openldap的权限问题
Mqant in-depth analysis
The author of surging issued the pressure test results
word使用技巧
[wechat applet] super easy to understand conditional rendering and list rendering
Swift - Generic
WildFly:如何从位于另一个应用程序中的 EJB 调用 EJB
C # explain out output parameters in detail
Xcode11 add a boot page (the launch images source option is missing after the upgrade)
51 single chip microcomputer to find out the input mode
Mature threads should know how to refuse
kettle5.4问题记录
Intensive reading series of papers
ospf防环