100 lines
3.5 KiB
C#
100 lines
3.5 KiB
C#
/********************************************************************************
|
||
* 文件名: ThirdPersonController.cs
|
||
* 全路径: \Script\Player\ThirdPersonController.cs
|
||
* 创建人: 李嘉
|
||
* 创建时间:2013-11-05
|
||
*
|
||
* 功能说明: 第三人称视角移动类
|
||
* 之后正式版中如果改为手指输入则将该类替换掉即可
|
||
* 修改记录:
|
||
*********************************************************************************/
|
||
/*
|
||
using Games.Scene;
|
||
using UnityEngine;
|
||
|
||
// 注:这玩意实际只提供方向,不提供速度
|
||
// 早期速度会只影响角色移动步长 - 刚开始步长较小,然后步长逐渐变大,实际每帧更新的话,只需要一个小步长即可
|
||
public class ThirdPersonController : MonoBehaviour
|
||
{
|
||
// 摇杆预测移动距离
|
||
private const float _joyStickMoveDelta = 2f;
|
||
|
||
/// <summary>
|
||
/// 获得当前Ui空间的摇杆/键盘输入方向
|
||
/// </summary>
|
||
public Vector2 GetCurrentInputDirection()
|
||
{
|
||
var result = Vector2.zero;
|
||
if (JoyStickLogic.Instance() != null)
|
||
result = JoyStickLogic.Instance().CurrentInput;
|
||
// 编辑器和PC端支持键盘和其他设备输入
|
||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||
if (result == Vector2.zero)
|
||
{
|
||
result.x = Input.GetAxis("Horizontal");
|
||
result.y = Input.GetAxis("Vertical");
|
||
if (result.x > 0f)
|
||
result.x = 1f;
|
||
else if (result.x < 0f)
|
||
result.x = -1f;
|
||
if (result.y > 0f)
|
||
result.y = 1f;
|
||
else if (result.y < 0f)
|
||
result.y = -1f;
|
||
if (result != Vector2.zero)
|
||
result = result.normalized;
|
||
}
|
||
#endif
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获得当前世界空间的摇杆/键盘输入方向
|
||
/// </summary>
|
||
public Vector3 GetCurrentInputDirectionInWorld()
|
||
{
|
||
var result = Vector3.zero;
|
||
Transform cameraTransform = null;
|
||
if (SceneLogic.CameraController != null && SceneLogic.CameraController.MainCamera != null)
|
||
cameraTransform = SceneLogic.CameraController.MainCamera.transform;
|
||
if (cameraTransform != null)
|
||
{
|
||
var inputDirection = GetCurrentInputDirection();
|
||
if (inputDirection != Vector2.zero)
|
||
{
|
||
var x = Vector3.Cross(Vector3.up, cameraTransform.forward);
|
||
if (x != Vector3.zero)
|
||
{
|
||
var z = Vector3.Cross(x, Vector3.up);
|
||
result = inputDirection.x * x + inputDirection.y * z;
|
||
}
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
|
||
public bool GetCurrentTargetPoint(out Vector3 movePosition)
|
||
{
|
||
var result = false;
|
||
movePosition = Vector3.zero;
|
||
var mainPlayer = Singleton<ObjManager>.GetInstance().MainPlayer;
|
||
if (mainPlayer != null)
|
||
{
|
||
// 摇杆和键盘输入优先
|
||
var joyStickInput = GetCurrentInputDirectionInWorld();
|
||
if (joyStickInput != Vector3.zero)
|
||
{
|
||
result = true;
|
||
movePosition = mainPlayer.Position + joyStickInput * _joyStickMoveDelta;
|
||
}
|
||
else if (ProcessInput.Instance() != null && ProcessInput.Instance().IsTouchMove)
|
||
{
|
||
result = true;
|
||
movePosition = ProcessInput.Instance().MovePoint;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
}
|
||
*/ |