当前位置:网站首页>[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组件
边栏推荐
- leetcode977. 有序数组的平方
- Gin框架原理
- C# List 集合对象去重 Linq去重 带时间去重
- [pits]packets go:428: busy buffer invalid connection
- How to open the applet for people near wechat (the way to open the applet near wechat)
- sql中的substr与substring函数用法
- Cabasicanimation pause \ enterprise action
- PowerDesigner显示Comment注释
- OSPF的优化和配置
- Machine learning 09: unsupervised learning
猜你喜欢

分布式笔记(02)— 分布式缓存之 Redis(用途、特点、高可用方案 Redis Cluster、Twemproxy、Codis 简要说明)

Wechat e-book reading applet graduation project (8) graduation project thesis template

模拟服务器进行请求

MAUI 框架入门学习05 MVVM数据模型理解

ospf综合实验

The author of surging issued the pressure test results

微信附近的人小程序怎么开(开通附近小程序的方法)

SQL interface switching cannot obtain focus

mysql中的视图、触发器和存储过程

Optimization and configuration of OSPF
随机推荐
C# List 集合对象去重 Linq去重 带时间去重
OSPF routing control, anti ring related knowledge
VirtualLab基础实验教程-7.偏振(3)
AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
Cannot find module ‘process‘ or its corresponding type declarations.
word使用技巧
[pits]packets go:428: busy buffer invalid connection
[Huang ah code] Introduction to MySQL - 5. Database tips: a single column group by will, and multiple columns?
AttributeError: ‘NoneType‘ object has no attribute ‘sort‘
nodejs-uuid
64. 最小路径和:给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。
成熟的线程要懂得拒绝
Wechat e-book reading applet graduation design of applet completion works (3) background function
ASP. Net1==visual studio create asp net demo
B+树存储过程、触发器、Substring和substr的区别及Truncate和Delete的区别
High performance and economy: aoteng persistent memory helps mobile cloud cope with severe memory challenges
【每日一题】剑指 Offer II 041. 滑动窗口的平均值
Simple explanation of C constructors
WildFly:如何从位于另一个应用程序中的 EJB 调用 EJB
Apache22与opencms9.5集成配置