unity中人物旋转
来源:互联网 发布:极品飞车18mac 编辑:程序博客网 时间:2024/03/28 18:47
1、通过获取鼠标经过点来旋转
using UnityEngine;
using System.Collections;
///
/// 人物模型转动
///
public class HeroModelRotate : MonoBehaviour
{
///
/// 第一次按下的位置
///
private Vector2 first = Vector2 .zero;
///
/// 鼠标的拖拽位置(第二次的位置)
///
private Vector2 second = Vector2 .zero;
///
/// 旋转的角度
///
private float angle = 3f;
void OnGUI(){ if (Event.current.type == EventType.MouseDown) { //记录鼠标按下的位置 first = Event.current.mousePosition; } if (Event.current.type == EventType.MouseDrag) { //记录鼠标拖动的位置 second = Event.current.mousePosition; if (second.x < first.x) { //拖动的位置的x坐标比按下的位置的x坐标小时,响应向左事件 this.transform.Rotate(Vector3.up, angle); } if (second.x > first.x) { //拖动的位置的x坐标比按下的位置的x坐标大时,响应向右事件 this.transform.Rotate(Vector3.down, angle); } first = second; }}
}
2、通过unity中自带的函数IDragHandler,IDragHandler, IBeginDragHandler, IEndDragHandler,
IPointerDownHandler建一个脚本,继承这个接口,实现这个接口,注册事件,用回调来实现,写一个回调类
public class TouchEX : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler,
IPointerDownHandler
{
public Action DragCallback;
public Action BeginDragCallback;
public Action EndDragCallback;
public Action PointerDownCallback;
public void OnBeginDrag(PointerEventData eventData)
{
if (BeginDragCallback != null)
{
BeginDragCallback.Invoke(eventData);
}
}
public void OnDrag(PointerEventData eventData){ if (DragCallback != null) { DragCallback.Invoke(eventData); }}public void OnEndDrag(PointerEventData eventData){ if (EndDragCallback != null) { EndDragCallback.Invoke(eventData); }}public void OnPointerDown(PointerEventData eventData){ if (PointerDownCallback != null) { PointerDownCallback.Invoke(eventData); }}
}
在unity中建一个Image,附上这个脚本,在写一个脚本,获取这个组件,注册事件,实现旋转
var touchLayer = GameObject.Find(“Touch”).GetComponent();
touchLayer.DragCallback = onModelDrag;
private void onModelDrag(PointerEventData eventData)
{
_modelPlace.transform.Rotate(new Vector3(0, -eventData.delta.x * Speed, 0));
}
- unity中人物旋转
- unity人物旋转和移动
- Unity相机旋转和人物朝向
- Unity中实现人物平滑转身
- Unity中实现人物平滑转身
- 第一人称和第三人称游戏中人物的移动和旋转
- 摄像机围绕人物旋转
- 人物平滑旋转
- Unity中鼠标点击地面控制人物移动
- Unity中鼠标点击地面控制人物移动
- Unity中武器与人物的碰撞检测
- 【Unity】unity 人物动态头像
- unity人物移动
- unity人物血条
- unity人物移动
- Unity人物残影
- Unity人物换装
- unity人物换装
- laravel中复杂的sql查询
- adb测试脚本
- MySQL性能优化总结
- Hive中的insert into语句
- Halcon中“Not enough memory available”异常处理方法之一
- unity中人物旋转
- IDEA快捷键
- Java Security Architecture--Java安全体系技术文档翻译目录贴
- 第一次机房收费-运行时错误'91'
- 安装 wbemcli
- Painter's Problem POJ
- 软件测试概念
- IOCP的一些思考(粘包,断包的处理)
- Thinkpad 电源标尺灰底解决方案