分享EasyTouch人物移动摇杆和摄像机45度朝向的控制和操作代码

原理不知道哦,之后再补上吧

 

直接上代码

(调用方法-则只需要在遥杆组件: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));
    }

}

参考:

Unity插件研究-EasyTouch V5 - 阿土仔 - 博客园 (cnblogs.com)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

avi9111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值