原理不知道哦,之后再补上吧
直接上代码
(调用方法-则只需要在遥杆组件:ETC Component,添加Event即可,也可以自己写一个桥接)
using HedgehogTeam.EasyTouch;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SPlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
public void OnEasyTouchMove(Vector2 delta)
{
Debug.Log("easy touch move" );
Debug.Log(delta);
float joyPositionX = delta.x;
float joyPositionY = delta.y;
//设置人物朝向(4种方法)
//transform.LookAt(new Vector3(transform.position.x + joyPositionX, transform.position.y, transform.position.z + joyPositionY));
// TickRotate(new Vector3(joyPositionX, transform.position.y, joyPositionY), transform);
CamDirectRotate(delta, transform);
//CamQuaternionRotate(transform);
//移动
transform.Translate(Vector3.forward * Time.deltaTime * 10);
}
/// <summary>
/// 摄像机的Transform通过摇杆输出的方向
/// </summary>
/// <param name="t">摄像机的transform</param>
/// <param name="dir">摇杆的方向</param>
/// <returns></returns>
public static Vector2 Transform2Dir(Transform t, Vector2 dir)
{
//注意:1、摄像机的Y轴角度和摇杆的坐标系是反方向的 2、角度和弧度
var f = Mathf.Deg2Rad * (-t.rotation.eulerAngles.y);
//方向标准化
dir.Normalize();
//旋转角度
var ret = new Vector2(dir.x * Mathf.Cos(f) - dir.y * Mathf.Sin(f), dir.x * Mathf.Sin(f) + dir.y * Mathf.Cos(f));
return ret;
}
public void CamDirectRotate(Vector2 delta,Transform transform)
{
Vector3 camDir = Camera.main.transform.forward;
Vector2 amount = Transform2Dir(Camera.main.transform,delta);
Vector3 pos = transform.position;
transform.LookAt(new Vector3(pos.x + amount.x, pos.y, pos.z + amount.y));
}
public void CamQuaternionRotate(Transform transform) {
Vector3 lastDirection = Camera.main.transform.forward;
transform.rotation = transform.rotation * Quaternion.LookRotation(lastDirection);
}
public void TickRotate(Vector3 sThisSyncDir,Transform objTransform)
{
sThisSyncDir = 15 * sThisSyncDir;//乘大15并没有什么作用,暂时发现这句代码没用的,只是测试用
Quaternion DestQuaternion = Quaternion.LookRotation(sThisSyncDir);
Quaternion sMidQuater = Quaternion.Lerp(objTransform.rotation, DestQuaternion, 3 * Time.deltaTime);//在from和to之间插值旋转.
objTransform.rotation = sMidQuater;
}
public void CharacterRotate(Vector2 amount,Transform transform) {
Vector3 pos = transform.position;
transform.LookAt(new Vector3(pos.x + amount.x, pos.y, pos.z + amount.y));
}
}