153 lines
3.6 KiB
C#
153 lines
3.6 KiB
C#
using SHADE;
|
|
using System;
|
|
|
|
//in air controls?
|
|
|
|
public class PlayerController : Script
|
|
{
|
|
public enum RaccoonStates
|
|
{
|
|
IDILE,
|
|
WALKING,
|
|
RUNNING,
|
|
INAIR,
|
|
FALLING,
|
|
HOLDING,
|
|
CAUGHT,
|
|
TOTAL
|
|
}
|
|
|
|
private RigidBody rb;
|
|
private float xAxisMove;
|
|
private float zAxisMove;
|
|
private RaccoonStates currentState = RaccoonStates.IDILE;
|
|
|
|
public float maxMoveVel = 2.0f;
|
|
public float moveForce = 200.0f;
|
|
public float sprintMultiplier = 1.5f;
|
|
|
|
public float jumpForce = 500.0f;
|
|
public float initialJumpForce = 100.0f;
|
|
public float maxJumpForce = 500.0f;
|
|
public float maxJumpTime = 500.0f;
|
|
private bool isJumping = false;
|
|
private bool isGrounded = false;
|
|
|
|
public PlayerController(GameObject gameObj) : base(gameObj) { }
|
|
|
|
protected override void awake()
|
|
{
|
|
rb = GetComponent<RigidBody>();
|
|
if (rb == null)
|
|
{
|
|
Debug.LogError("RigidBody is NULL!");
|
|
}
|
|
|
|
RaccoonStates currentState = RaccoonStates.IDILE;
|
|
}
|
|
|
|
protected override void update()
|
|
{
|
|
currentState = RaccoonStates.IDILE;
|
|
|
|
Move();
|
|
Sprint();
|
|
Jump();
|
|
|
|
if (rb != null && currentState == RaccoonStates.IDILE)
|
|
rb.LinearVelocity = new Vector3(0.0f, 0.0f, 0.0f);
|
|
|
|
Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString());
|
|
}
|
|
|
|
private void Move()
|
|
{
|
|
|
|
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;
|
|
|
|
if((Input.GetKey(Input.KeyCode.A) || Input.GetKey(Input.KeyCode.D) || Input.GetKey(Input.KeyCode.W) || Input.GetKey(Input.KeyCode.S)) && currentState != RaccoonStates.RUNNING)
|
|
currentState = RaccoonStates.WALKING;
|
|
|
|
if (rb != null && currentState == RaccoonStates.WALKING)
|
|
{
|
|
if (rb.LinearVelocity.x <= maxMoveVel || rb.LinearVelocity.x >= -maxMoveVel)
|
|
rb.AddForce(new Vector3(moveForce * xAxisMove, 0.0f, 0.0f));
|
|
else
|
|
{
|
|
Vector3 v = rb.LinearVelocity;
|
|
if(v.x >= 0)
|
|
v.x = maxMoveVel;
|
|
else
|
|
v.x = -maxMoveVel;
|
|
|
|
rb.LinearVelocity = v;
|
|
}
|
|
|
|
if (rb.LinearVelocity.z <= maxMoveVel || rb.LinearVelocity.z >= -maxMoveVel)
|
|
rb.AddForce(new Vector3(0.0f, 0.0f, moveForce * zAxisMove ));
|
|
else
|
|
{
|
|
Vector3 v = rb.LinearVelocity;
|
|
if (v.z >= 0)
|
|
v.z = maxMoveVel;
|
|
else
|
|
v.z = -maxMoveVel;
|
|
rb.LinearVelocity = v;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Sprint()
|
|
{
|
|
//left shift not working for now and chang eto getkey down
|
|
if (Input.GetKey(Input.KeyCode.RightShift))
|
|
{
|
|
if (currentState == RaccoonStates.WALKING && rb != null)
|
|
{
|
|
currentState = RaccoonStates.RUNNING;
|
|
rb.LinearVelocity *= sprintMultiplier;
|
|
}
|
|
}
|
|
|
|
if (Input.GetKeyUp(Input.KeyCode.RightShift))
|
|
{
|
|
if (currentState == RaccoonStates.RUNNING && rb != null)
|
|
{
|
|
currentState = RaccoonStates.WALKING;
|
|
rb.LinearVelocity /= sprintMultiplier;
|
|
}
|
|
}
|
|
}
|
|
|
|
//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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|