SHADE_Y3/TempScriptsFolder/PlayerController.cs

172 lines
4.3 KiB
C#
Raw Normal View History

2022-10-30 23:59:35 +08:00
using SHADE;
using System;
2022-10-31 16:45:47 +08:00
//in air controls?
2022-10-30 23:59:35 +08:00
public class PlayerController : Script
{
2022-10-31 16:45:47 +08:00
public enum RaccoonStates
{
IDILE,
WALKING,
RUNNING,
INAIR,
FALLING,
HOLDING,
CAUGHT,
TOTAL
}
2022-10-30 23:59:35 +08:00
private RigidBody rb;
2022-11-01 01:31:13 +08:00
private Transform tranform;
public float xAxisMove;
public float zAxisMove;
private bool isMoveKeyPress = false;
public RaccoonStates currentState = RaccoonStates.IDILE;
2022-10-31 16:45:47 +08:00
public float maxMoveVel = 2.0f;
public float moveForce = 200.0f;
public float sprintMultiplier = 1.5f;
2022-11-01 01:31:13 +08:00
public float rotationFactorPerFrame = 1.0f;
2022-10-31 16:45:47 +08:00
public float jumpForce = 500.0f;
2022-11-01 01:31:13 +08:00
/* public float initialJumpForce = 100.0f;
2022-10-31 16:45:47 +08:00
public float maxJumpForce = 500.0f;
public float maxJumpTime = 500.0f;
private bool isJumping = false;
2022-11-01 01:31:13 +08:00
private bool isGrounded = false;*/
2022-10-30 23:59:35 +08:00
public PlayerController(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{
rb = GetComponent<RigidBody>();
if (rb == null)
Debug.LogError("RigidBody is NULL!");
2022-11-01 01:31:13 +08:00
tranform = GetComponent<Transform>();
if(tranform == null)
Debug.LogError("tranform is NULL!");
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
tranform.LocalRotation = Quaternion.Euler(0.0f,0.0f,0.0f);
2022-10-31 16:45:47 +08:00
RaccoonStates currentState = RaccoonStates.IDILE;
2022-10-30 23:59:35 +08:00
}
protected override void update()
{
2022-11-01 01:31:13 +08:00
if (Input.GetKey(Input.KeyCode.G))
{
tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
}
2022-10-31 16:45:47 +08:00
2022-11-01 01:31:13 +08:00
MoveKey();
//Rotation();
2022-10-30 23:59:35 +08:00
Move();
2022-10-31 16:45:47 +08:00
Sprint();
2022-11-01 01:31:13 +08:00
//Jump();
2022-10-30 23:59:35 +08:00
2022-10-31 16:45:47 +08:00
2022-11-01 01:31:13 +08:00
Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString());
2022-10-30 23:59:35 +08:00
}
2022-11-01 01:31:13 +08:00
private void MoveKey()
2022-10-30 23:59:35 +08:00
{
if (Input.GetKey(Input.KeyCode.A))
xAxisMove = -1;
else if (Input.GetKey(Input.KeyCode.D))
xAxisMove = 1;
else
xAxisMove = 0;
if (Input.GetKey(Input.KeyCode.W))
zAxisMove = -1;
else if (Input.GetKey(Input.KeyCode.S))
zAxisMove = 1;
else
zAxisMove = 0;
2022-11-01 01:31:13 +08:00
isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0;
if(isMoveKeyPress && currentState != RaccoonStates.RUNNING)
2022-10-31 16:45:47 +08:00
currentState = RaccoonStates.WALKING;
2022-11-01 01:31:13 +08:00
if (!isMoveKeyPress)
currentState = RaccoonStates.IDILE;
}
private void Move()
{
if (isMoveKeyPress && rb != null)
2022-10-31 16:45:47 +08:00
{
2022-11-01 01:31:13 +08:00
rb.AddForce(new Vector3(moveForce * xAxisMove, 0.0f, moveForce * zAxisMove));
if (rb.LinearVelocity.x > maxMoveVel || rb.LinearVelocity.x < -maxMoveVel)
2022-10-31 16:45:47 +08:00
{
Vector3 v = rb.LinearVelocity;
2022-11-01 01:31:13 +08:00
v.x = System.Math.Clamp(v.x, -maxMoveVel, maxMoveVel);
2022-10-31 16:45:47 +08:00
rb.LinearVelocity = v;
}
2022-11-01 01:31:13 +08:00
if (rb.LinearVelocity.z > maxMoveVel || rb.LinearVelocity.z < -maxMoveVel)
2022-10-31 16:45:47 +08:00
{
Vector3 v = rb.LinearVelocity;
2022-11-01 01:31:13 +08:00
v.z = System.Math.Clamp(v.z, -maxMoveVel, maxMoveVel);
2022-10-31 16:45:47 +08:00
rb.LinearVelocity = v;
}
2022-11-01 01:31:13 +08:00
2022-10-31 16:45:47 +08:00
}
}
private void Sprint()
{
2022-11-01 01:31:13 +08:00
if (Input.GetKeyDown(Input.KeyCode.LeftShift) && currentState == RaccoonStates.WALKING && isMoveKeyPress)
2022-10-31 16:45:47 +08:00
{
2022-11-01 01:31:13 +08:00
currentState = RaccoonStates.RUNNING;
2022-10-31 16:45:47 +08:00
}
2022-11-01 01:31:13 +08:00
else if (Input.GetKeyUp(Input.KeyCode.LeftShift) && currentState == RaccoonStates.RUNNING && isMoveKeyPress)
2022-10-31 16:45:47 +08:00
{
2022-11-01 01:31:13 +08:00
currentState = RaccoonStates.WALKING;
2022-10-31 16:45:47 +08:00
}
}
//press and hold jump
private void Jump()
{
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE)
{
if (Input.GetKeyDown(Input.KeyCode.Space) && rb != null)
{
currentState = RaccoonStates.INAIR;
rb.AddForce(new Vector3(0.0f, jumpForce, 0.0f));
}
}
//collision check when grounded
2022-10-30 23:59:35 +08:00
}
2022-10-31 16:45:47 +08:00
2022-11-01 01:31:13 +08:00
private void Rotation()
{
Vector3 poitionToLookAt;
poitionToLookAt.x = xAxisMove;
poitionToLookAt.y = 0.0f;
poitionToLookAt.z = zAxisMove;
if (tranform != null)
{
Quaternion currentRotation = tranform.LocalRotation;
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING)
{
Quaternion targetRotation = Quaternion.LookRotation(poitionToLookAt, new Vector3(0.0f, 1.0f, 0.0f));
tranform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationFactorPerFrame * (float)Time.DeltaTime);
}
}
}
2022-10-31 16:45:47 +08:00
2022-10-30 23:59:35 +08:00
}