Created
January 21, 2020 13:32
-
-
Save Masterexa/82eb09fe4c3e4f97ab9ec81520641496 to your computer and use it in GitHub Desktop.
Unity : Simple Character Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Cinemachine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.InputSystem; | |
namespace Masterexa.CharactorControl { | |
/// <summary> | |
/// プレイヤー入力用のHumanoidMotorコントローラー( InputSystem, Cinemachine2 必須 ) | |
/// </summary> | |
public class HumanoidPlayerControl : MonoBehaviour { | |
#region Fields | |
[SerializeField] PlayerInput m_playerInput; | |
[SerializeField] HumanoidMotor m_motor; | |
[SerializeField,Range(0,1)] float m_lookWeight = 0f; | |
[SerializeField,Range(0,1)] float m_lookRate = 0f; | |
[SerializeField] CinemachineVirtualCameraBase m_thirdCamera; | |
[SerializeField] Transform m_lookPoint; | |
InputAction m_move; | |
#endregion | |
#region Events | |
private void Start() | |
{ | |
m_move = m_playerInput.actions["Move"]; | |
m_motor.EnabledLooking = true; | |
} | |
public void Update() | |
{ | |
var move = (Vector3)m_move.ReadValue<Vector2>(); | |
// Determine direction from camera angle. | |
var ori = m_thirdCamera.State.FinalOrientation; | |
move = (ori * Vector3.right) * move.x + (ori * Vector3.forward) * move.y; | |
move.y = 0f; | |
// Setup the parameters. | |
m_motor.Move(move); | |
m_motor.LookPoint = m_lookPoint.position; | |
m_motor.LookWeight = m_lookWeight; | |
m_motor.LookRate = m_lookRate; | |
} | |
#endregion | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace Masterexa.CharactorControl{ | |
/// <summary> | |
/// 人型キャラクターの動力 | |
/// <para> | |
/// コンポーネントに設定されたパラメーターから剛体を制御して、キャラクターを移動および回転させます。<br> | |
/// Move and rotate the character by controlling the rigid body from the parameters set in the component. | |
/// </para> | |
/// </summary> | |
public class HumanoidMotor : MonoBehaviour { | |
private static readonly Vector3 MASK_XZ = new Vector3(1, 0, 1); | |
#region Fields | |
[SerializeField] Rigidbody m_rigidbody; | |
/// <summary> | |
/// 移動先 | |
/// </summary> | |
public Vector3 MoveDestination{ get; set; } | |
/// <summary> | |
/// 移動速度 | |
/// </summary> | |
public float MoveSpeed{ get; set; } | |
/// <summary> | |
/// 注視を有効にするか | |
/// </summary> | |
public bool EnabledLooking{ get; set; } | |
/// <summary> | |
/// 注視点 | |
/// </summary> | |
public Vector3 LookPoint{ get; set; } | |
/// <summary> | |
/// 注視の強さ | |
/// </summary> | |
public float LookWeight{ get; set; } | |
/// <summary> | |
/// 注視のレート | |
/// </summary> | |
public float LookRate{ get; set; } | |
#endregion | |
#region Events | |
private void Awake() | |
{ | |
m_rigidbody = m_rigidbody ?? GetComponent<Rigidbody>(); | |
} | |
private void FixedUpdate() | |
{ | |
if (EnabledLooking) | |
{ | |
var lookDir = Vector3.Lerp(MoveDestination, LookPoint, LookWeight) - transform.position; | |
lookDir.Scale(MASK_XZ); | |
lookDir.Normalize(); | |
if (lookDir.sqrMagnitude > 0) | |
{ | |
transform.rotation = Quaternion.Lerp( | |
transform.rotation, | |
Quaternion.LookRotation(lookDir, Vector3.up), | |
Time.deltaTime * (1f / Mathf.Max(float.Epsilon, LookRate)) | |
); | |
} | |
} | |
m_rigidbody.position += (MoveDestination - transform.position).normalized * MoveSpeed * Time.deltaTime; | |
} | |
#endregion | |
#region Methods | |
public void StrafeRelative(Vector2 relativeVelocity) | |
{ | |
var V3 = new Vector3(relativeVelocity.x, 0f, relativeVelocity.y); | |
MoveDestination = transform.TransformPoint(V3); | |
MoveSpeed = V3.magnitude; | |
} | |
public void Move(Vector2 velocity) | |
{ | |
Move(new Vector3(velocity.x, 0f, velocity.y)); | |
} | |
public void Move(Vector3 velocity) | |
{ | |
MoveDestination = transform.position + velocity; | |
MoveSpeed = velocity.magnitude; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment