Update Night of Horror authored by Hong, Xin's avatar Hong, Xin
...@@ -3,6 +3,7 @@ title: Night of Horror ...@@ -3,6 +3,7 @@ title: Night of Horror
--- ---
[Toc] [Toc]
# Game Design Document # Game Design Document
...@@ -149,88 +150,45 @@ public class PlayerInput : MonoBehaviour ...@@ -149,88 +150,45 @@ public class PlayerInput : MonoBehaviour
} }
``` ```
```csharp ```csharp
using System; private void ResetCameraRotation()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovingState : PlayerState
{ {
protected Vector2 movementInput; Vector3 cameraForward = stateMachine.Player.cameraTransform.forward;
cameraForward.y = 0;
cameraForward.Normalize();
protected float movementSpeed = 3f; Quaternion rotation = Quaternion.LookRotation(cameraForward);
public PlayerMovingState(PlayerStateMachine playerStateMachine) : base(playerStateMachine)
{
}
public override void Enter()
{
base.Enter();
ResetCameraRotation(); stateMachine.Player.playerRigidbody.rotation = rotation;
} }
public override void Update()
{
base.Update();
MovementInput(); private void MovementInput()
} {
movementInput = stateMachine.Player.input.playerActions.Movement.ReadValue<Vector2>();
}
public override void PhysicsUpdate() private void Move()
{ {
base.PhysicsUpdate(); Vector3 cameraForward = stateMachine.Player.cameraTransform.forward;
cameraForward.y = 0;
cameraForward.Normalize();
Move(); Vector3 cameraRight = stateMachine.Player.cameraTransform.right;
} cameraRight.y = 0;
cameraRight.Normalize();
public override void Exit() Vector3 moveDirection = cameraForward * movementInput.y + cameraRight * movementInput.x;
{
base.Exit();
}
private void ResetCameraRotation() if (moveDirection != Vector3.zero)
{ {
Vector3 cameraForward = stateMachine.Player.cameraTransform.forward; Quaternion targetRotation = Quaternion.LookRotation(moveDirection);
cameraForward.y = 0; float rotationSpeed = 0.5f;
cameraForward.Normalize(); stateMachine.Player.playerRigidbody.rotation = Quaternion.Slerp(stateMachine.Player.playerRigidbody.rotation, targetRotation, rotationSpeed * Time.deltaTime);
Quaternion rotation = Quaternion.LookRotation(cameraForward);
stateMachine.Player.playerRigidbody.rotation = rotation; stateMachine.Player.playerRigidbody.velocity = moveDirection.normalized * movementSpeed;
} }
else
private void MovementInput()
{
movementInput = stateMachine.Player.input.playerActions.Movement.ReadValue<Vector2>();
}
private void Move()
{ {
Vector3 cameraForward = stateMachine.Player.cameraTransform.forward; stateMachine.Player.playerRigidbody.velocity = Vector3.zero;
cameraForward.y = 0;
cameraForward.Normalize();
Vector3 cameraRight = stateMachine.Player.cameraTransform.right;
cameraRight.y = 0;
cameraRight.Normalize();
Vector3 moveDirection = cameraForward * movementInput.y + cameraRight * movementInput.x;
if (moveDirection != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(moveDirection);
float rotationSpeed = 0.5f;
stateMachine.Player.playerRigidbody.rotation = Quaternion.Slerp(stateMachine.Player.playerRigidbody.rotation, targetRotation, rotationSpeed * Time.deltaTime);
stateMachine.Player.playerRigidbody.velocity = moveDirection.normalized * movementSpeed;
}
else
{
stateMachine.Player.playerRigidbody.velocity = Vector3.zero;
}
} }
} }
``` ```
... ...
......