模拟人的手指在UI上滑动时3D模型跟随着移动(Unity)
问题:
怎么让当手指滑动的同时对应的模型发生旋转
解决办法:
1:通过控制摄像机或者模型来实现效果
2:通过获取鼠标移动时X轴Y轴的偏移量来确定模型的旋转角度
3:为了不让人感觉到突兀,建议使用Mathf.SmoothDamp方法实现角度的改变
实现代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class ChinarSmoothUi3DCamera : MonoBehaviour { public Transform point;
private Vector3 Tras = Vector3.zero; public float distance = 10.0f;
public float minDistance = 2f;
public float maxDistance = 15f;
public float zoomSpeed = 1f;
public float xSpeed = 250.0f;
public float ySpeed = 250.0f;
public bool allowYTilt = true;
public float yMinLimit = -90f;
public float yMaxLimit = 90f; private float x = 0.0f;
private float y = 0.0f;
private float targetX = 0f;
private float targetY = 0f;
public float targetDistance = 0f;
private float xVelocity = 1f;
private float yVelocity = 1f;
private float zoomVelocity = 1f;
void Start () { Vector3 tange = transform.eulerAngles;
targetX = x = tange.x;
//targetY = y = ClampAngle(tange.y, yMinLimit, yMaxLimit);
targetDistance = distance; } // Update is called once per frame void LateUpdate() {
if(point==null)
{
return;
}
if(Input.GetAxis("Mouse ScrollWheel")>0)
{
targetDistance -= zoomSpeed;
}
if(Input.GetAxis("Mouse ScrollWheel")<0)
{
targetDistance += zoomSpeed;
}
targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);
if(Input.GetMouseButton(1))
{ targetX+= Input.GetAxis("Mouse X") * xSpeed * 0.02f;
if (allowYTilt)
{
//targetY -= Input.GetAxis("Mouse Y") * xSpeed * 0.02f;
//targetY= ClampAngle(targetY, yMinLimit, yMaxLimit);
}
}
x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);
//y = allowYTilt ? Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f) : targetY;
//Quaternion rotation = Quaternion.Euler(y, x, 0);
Quaternion rotation = Quaternion.Euler(0, x, 0);
distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + point.position + Tras;
transform.rotation = rotation;
transform.position = position; } public float ClampAngle(float angle,float min,float max)
{
if(angle>360)
{
angle -= 360;
}
if(angle<-360)
{
angle += 360;
}
return Mathf.Clamp(angle, min, max);
}
}
模拟人的手指在UI上滑动时3D模型跟随着移动(Unity)的更多相关文章
- Android ScrollView里嵌套RecyclerView时,在RecyclerView上滑动时出现卡顿(冲突)的现象
最近在项目中遇到一个现象,一个界面有一个RecyclerView(GridView型的),外面套了一层ScrollView,通过ScrollView上下滚动,但是在滑动的时候如果是在RecyclerV ...
- Mask裁切UI粒子特效或者3D模型
刚好前几天有人问我这个问题,再加上新项目也可能用,所以这两天就研究了一下.其实如果粒子特效 和3D模型 都用RenderTexture来做的话就不会有裁切的问题,但是粒子特效用RenderTextur ...
- 【安卓】给gallery内"控件"挂载事件,滑动后抬起手指时也触发事件(滑动时不应触发)的解决、!
思路: 1.gallery内控件挂载事件(如:onClickListener)的方法类似listview,可直接在baseAdapter.getView内给控件挂载(详细方法百度). 2.貌似没问题, ...
- Unity在UI界面上显示3D模型/物体,控制模型旋转
Unity3D物体在UI界面的显示 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- Flash Stage3D 在2D UI 界面上显示3D模型问题完美解决
一直以来很多Stage3D开发者都在为3D模型在2DUI上显示的问题头疼.Stage3D一直是在 Stage2D下面.为了做到3D模型在2DUI上显示通常大家有几种实现方式,下面来说说这几种实现方式吧 ...
- Unity3D_UGUI判断鼠标或者手指是否点击在UI上
比如战斗场景,UI和3D场景同时都需要响应触摸事件,如果同时响应可能就会出现触摸UI的时候影响到了3D部分.为了解决这个问题在判断3D响应之前要先判断手指是否点击在UI上. 以前NGUI的时候都是自己 ...
- Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏
效果图: 源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- 结合 CSS3 & Canvas 模拟人行走的效果
HTML5 和 CSS3 技术给 Web 带来了新的利器,点燃了 Web 开发人员的激情.所谓只有想不到,没有做不到,的确如此.下面给大家分享一个结合 CSS3 & Canvas 模拟人行走的 ...
- unity区分点击在3D物体还是2D UI上
当场景中的3D物体需要响应点击,但同时有UI显示时,存在判断点击是在3D物体上还是UI上的问题,办法如下: 1. 射线检测所有2D 3D物体,有2D物体被检测到时表明当前有UI.但无论Physics2 ...
随机推荐
- 关于Linux的本地回环路由lo [127.0.0.1 ]
最近 打算配开发板的socket通讯,打印环境变量发现却没有 127.0.0.1 / # ifconfig -a eth0 Link encap:Ethernet HWaddr 86:43:C9:A1 ...
- 用c实现shell排序
shell排序的方法又称缩小增量法,是对直接插入排序法的改进.至于对于分组后采用哪种排序方法实现,本例采用直接选择排序和直接插入排序,理论上讲,通过分组排序后,数据基本上有序,这时通过直接插入排序会比 ...
- 运维系列之二 Linux文件种类和扩展名
一.文件种类 1.普通文件 用ls查看文件属性时,显示的是[-] 2.目录文件(directory) 文件属性第一个为[d] 3.连接文件(link) 类似于win下的快捷方式,文件第一个属性为[l] ...
- K - Count the string kmp_Next数组应用
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- [Cypress] Create a Single Custom Cypress Command from Multiple Commands
Cypress provides a straightforward API that allows you to define custom commands. In this lesson, we ...
- 初触Python,关于pyquery解析html(百度贴吧)
一直听同事说Python是个奇妙的语言,上周在逛知乎的时候深受这个话题的启示. 能利用爬虫技术做到哪些非常酷非常有趣非常实用的事情? 先是说到IDE的选择,作为python新人,尽管知道mac终端自带 ...
- hibernate实战笔记1---初探
因为在学习Spring的时候学到有关数据库的章节的时候,提及到了hibernate的集成,可是我对hibernate技术差点儿是一点不了解.仅仅是知道它是一个orm对象映射框架,所以在初探的章节做一下 ...
- extjs_07_combobox&tree&chart
1.combobox <%@ page language="java" import="java.util.*" pageEncoding="U ...
- UVA LIVE 7146 Defeat the Enemy
这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn. ...
- iOS - 社会化分享-微信分享,朋友圈分享
我仅仅做了文字和图片分享功能 1. TARGETS - Info - URL Types identifier -> weixin URL Schemes -> 应用id 2.在AppD ...