EasyTouch插件,摄像机旋转后,物体移动方向不一致
using EasyTouch;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RoleCtrl : MonoBehaviour
{
/// <summary>
/// 控制器
/// </summary>
[HideInInspector]
public CharacterController CharacterController;
void OnEnable()
{
EasyJoystick.On_JoystickMove += OnJoystickMove;
EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;
}
// 此函数是摇杆移动中所要处理的事
void OnJoystickMove(MovingJoystick move)
{
if (move.joystickName != "MoveJoystick") // 在这里的名字new joystick 就是上面所说的很重要的名字,在上面图片中joystickName的你修改了什么名字,这里就要写你修改的好的名字(不然脚本不起作用)。
{
return;
}
float x = move.joystickAxis.x;
float z = move.joystickAxis.y;
if (x != 0 || z != 0)
{
//就是这里控制,重要!!!
Vector3 targetDirection = new Vector3(x, 0, z);
float y = Camera.main.transform.rotation.eulerAngles.y;
targetDirection = Quaternion.Euler(0, y, 0) * targetDirection;
transform.LookAt(targetDirection + transform.position);
//////transform.Translate(targetDirection * Time.deltaTime * 6f, Space.World);
Vector3 moment = targetDirection * Time.deltaTime * 5;
GetComponent<CharacterController>().Move(moment);
GetComponent<Animator>().Play("walk");
}
}
//移动摇杆结束
void OnJoystickMoveEnd(MovingJoystick move)
{
GetComponent<Animator>().Play("idel");
}
void Start()
{
CharacterController = GetComponent<CharacterController>();
}
void Update()
{
if (CharacterController == null) return;
//让角色贴着地面
if (!CharacterController.isGrounded)
{
CharacterController.Move((transform.position + new Vector3(0, -1000, 0)) - transform.position);
}
if (CharacterController != null)
{
CameraAutoFollow();
}
}
#region CameraAutoFollow 摄像机自动跟随
/// <summary>
/// 摄像机自动跟随
/// </summary>
private void CameraAutoFollow()
{
if (CameraCtrl.Instance == null) return;
CameraCtrl.Instance.transform.position = gameObject.transform.position;
CameraCtrl.Instance.AutoLookAt(gameObject.transform.position);
}
#endregion
}

浙公网安备 33010602011771号