using System;
namespace TripMax.Character
{
public class CharacterMotion : MonoBehaviour
{
private CharacterController controller;
[Tooltip("旋转速度")]
public float roateSpeed = 20f;
[Tooltip("移动速度")]
public float moveSpeed = 2f;
private void Start()
{
controller = GetComponent<CharacterController>();
}
public void LookAtDir(Vector3 direction)
{
if(direction == Vector3.zero) return;
Quaternion lookDir = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, lookDir, roateSpeed * Time.deltaTime);
}
public void Movement(Vector3 direction)
{
LookAtDir(direction);
Vector3 forward = transform.foward;
forward.y = -1;
controller.Move(forward * Time.deltaTime * moveSpeed);
}
}
public class InputController : MonoBeahviour
{
private ETCJoystick joys;
private CharacterMotion motion;
private Animator animator;
private PlayerStatus status;
private ETCButton[] skillButtons;
private void Awake()
{
joys = FindObjectOfType<ETCJoystick>();
motion = GetComponent<CharacterMotion>();
animator = GetComponentInChildren<Animator>();
status = GetComponent<PlayerStatus>();
skillButtons = FindObjectsOfType<ETCButton>();
}
private void OnEnable()
{
joys.onMove.AddListener(OnJoystickMove);
joys.onMoveStart.AddListener(onJoysStart);
joys.onMoveEnd.AddListener(onJoysEnd);
for(int i=0; i<skillButtons.Length; i++)
{
skillButtons[i].onDown.AddListnener(onSkillButtonDown);
}
}
private void OnDisable()
{
joys.onMove.RemoveListener(OnJoystickMove);
joys.onMoveStart.RemoveListener(onJoysStart);
joys.onMoveEnd.RemoveListener(onJoysEnd);
for(int i=0; i<skillButtons.Length; i++)
{
skillButtons[i].onDown.RemoveListnener(onSkillButtonDown);
}
}
private void OnJoystickMove(Vector2 dir)
{
motion.Movement(new Vector3(dir.x, 0, dir.y));
}
private void OnJoysStart()
{
animator.SetBool(status.animationParam.run, true);
}
private void onJoysEnd()
{
animator.SetBool(status.animationParam.run, false);
}
private void OnSkillButtonDown()
{
print("打死你");
}
}
}