Lean_Touch控制移动
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Lean.Touch;
public class Lean_Touch: MonoBehaviour
{
public float speed=100;//定义速度上下
private Rigidbody _rig;//定义移动刚体
private Renderer _render;//定义材质渲染
private float offsets = 0;//定义渲染变量
//初始化
public void Awake()
{
//得到组件
_rig = GetComponent<Rigidbody>();
_render = GetComponent<Renderer>();
}
//开启启动
protected virtual void OnEnable()
{
//监听加事件
LeanTouch.OnGesture += OnGesture;
}
//关闭禁用
protected virtual void OnDisable()
{
//监听去事件
LeanTouch.OnGesture -= OnGesture;
}
//List<LeanFinger> fingers参数的方法
public void OnGesture(List<LeanFinger> fingers)
{
//判断上划
if (LeanGesture.GetScaledDelta(fingers).x > 0 )
{
//材质球偏移量
offsets += 0.1f;
_render.material.mainTextureOffset = new Vector2(offsets, 0);
//给物体x加力
_rig.AddForce(new Vector3(speed, 0, 0), ForceMode.Force);
}
//判断下划
if (LeanGesture.GetScaledDelta(fingers).x < 0 )
{
//材质球偏移量
offsets -= 0.1f;
_render.material.mainTextureOffset = new Vector2(offsets, 0);
//给物体x加反向力
_rig.AddForce(new Vector3(-speed, 0, 0), ForceMode.Force);
}
//判断左划
if (LeanGesture.GetScaledDelta(fingers).y > 0 )
{
//给物体x加反向力
_rig.AddForce(new Vector3(0, 0, speed), ForceMode.Force);
}
//判断右划
if (LeanGesture.GetScaledDelta(fingers).y < 0 )
{
//给物体x加反向力
_rig.AddForce(new Vector3(0, 0, -speed), ForceMode.Force);
}
//根据滑动方向给物体选择面朝方向
Vector3 movePosition = new Vector3(LeanGesture.GetScaledDelta(fingers).x, 0, LeanGesture.GetScaledDelta(fingers).y);
Vector3 dir = transform.position + movePosition;
transform.LookAt(dir);
}
}
注:此移动方式是在LeanTouch插件下进行的移动旋转、移动加力方式根据需要调节、ForceMode.Force:给物体添加一个持续的力并使用其质量、ForceMode.Acceleration:给物体添加一个持续的加速度,但是忽略其质量、 ForceMode.Impulse:给物体添加一个瞬间的力并使用其质量、ForceMode.VelocityChange:给物体添加一个瞬间的加速度,但是忽略其质量。