PlayerController and PickAndThrow #167

Merged
glencelow merged 21 commits from PlayerController into main 2022-11-02 17:40:51 +08:00
1 changed files with 56 additions and 29 deletions
Showing only changes of commit 97a39d62c6 - Show all commits

View File

@ -10,7 +10,7 @@ public class PlayerController : Script
IDILE, IDILE,
WALKING, WALKING,
RUNNING, RUNNING,
INAIR, JUMP,
FALLING, FALLING,
HOLDING, HOLDING,
CAUGHT, CAUGHT,
@ -19,26 +19,47 @@ public class PlayerController : Script
private RigidBody rb; private RigidBody rb;
private Transform tranform; private Transform tranform;
private int xAxisMove;
private int zAxisMove; //to be remove
public float drag = 2.0f; public float drag = 2.0f;
private bool isMoveKeyPress = false; [SerializeField]
[Tooltip("The current state fo the raccoon")]
public RaccoonStates currentState = RaccoonStates.IDILE; public RaccoonStates currentState = RaccoonStates.IDILE;
//Movement variables============================================================
[SerializeField]
[Tooltip("Max vel for walking")]
public float maxMoveVel = 2.0f; public float maxMoveVel = 2.0f;
[SerializeField]
[Tooltip("how much force is apply for walking")]
public float moveForce = 50.0f; public float moveForce = 50.0f;
[SerializeField]
[Tooltip("increase the moveForce and maxMoveVel by its amt")]
public float sprintMultiplier = 2.0f; public float sprintMultiplier = 2.0f;
private float oldForce; private float oldForce;
private float maxOldVel; private float maxOldVel;
private bool sprintIncreaseOnce = false; private bool sprintIncreaseOnce = false;
private int xAxisMove;
private int zAxisMove;
private bool isMoveKeyPress = false;
[SerializeField]
[Tooltip("curr not working")]
public float rotationFactorPerFrame = 1.0f; public float rotationFactorPerFrame = 1.0f;
//Jumping vars==================================================================
public float initialJumpVel; [SerializeField]
public float maxJumpHeight = 1.0f; [Tooltip("max height of the jump")]
public float maxJumpTime = 0.5f; public float maxJumpHeight = 4.0f;
private bool isJumping = false; [SerializeField]
public bool isGrounded = true; [Tooltip("max amt of time it will take for the jump")]
public float maxJumpTime = 0.75f;
[SerializeField]
[Tooltip("increase gravity when falling")]
public float fallMultipler = 2.0f;
private float initialJumpVel;
private bool isGrounded = true;
private float gravity = -9.8f; private float gravity = -9.8f;
private float groundGravity = -0.5f; private float groundGravity = -0.5f;
@ -80,7 +101,7 @@ public class PlayerController : Script
MoveKey(); MoveKey();
Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString()); //Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString());
} }
protected override void fixedUpdate() protected override void fixedUpdate()
@ -111,10 +132,10 @@ public class PlayerController : Script
isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0;
if(isMoveKeyPress && currentState != RaccoonStates.RUNNING) if(isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded)
currentState = RaccoonStates.WALKING; currentState = RaccoonStates.WALKING;
if (!isMoveKeyPress) if (!isMoveKeyPress && isGrounded)
currentState = RaccoonStates.IDILE; currentState = RaccoonStates.IDILE;
} }
@ -144,7 +165,7 @@ public class PlayerController : Script
private void Sprint() private void Sprint()
{ {
if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress) if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded)
{ {
currentState = RaccoonStates.RUNNING; currentState = RaccoonStates.RUNNING;
if(!sprintIncreaseOnce) if(!sprintIncreaseOnce)
@ -158,7 +179,7 @@ public class PlayerController : Script
} }
} }
if (Input.GetKeyUp(Input.KeyCode.LeftShift) && (currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE)) if (Input.GetKeyUp(Input.KeyCode.LeftShift))
{ {
if(isMoveKeyPress) if(isMoveKeyPress)
currentState = RaccoonStates.WALKING; currentState = RaccoonStates.WALKING;
@ -173,23 +194,17 @@ public class PlayerController : Script
{ {
if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE) if (currentState == RaccoonStates.WALKING || currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE)
{ {
if (Input.GetKeyDown(Input.KeyCode.Space)&& isGrounded && rb != null) if (Input.GetKeyDown(Input.KeyCode.Space) && isGrounded && rb != null)
{ {
isJumping = true; currentState = RaccoonStates.JUMP;
Vector3 v = rb.LinearVelocity; Vector3 v = rb.LinearVelocity;
v.y = initialJumpVel; v.y = initialJumpVel * 0.5f;
rb.LinearVelocity = v; rb.LinearVelocity = v;
} }
} }
if (rb != null) if(rb != null && !isGrounded && (rb.LinearVelocity.y < 0.0f || Input.GetKeyUp(Input.KeyCode.Space)))
{ currentState = RaccoonStates.FALLING;
if (rb.LinearVelocity.y > 0 && isJumping)
currentState = RaccoonStates.INAIR;
else if(rb.LinearVelocity.y < 0 && isJumping)
currentState = RaccoonStates.FALLING;
}
} }
private void Rotation() private void Rotation()
@ -215,7 +230,7 @@ public class PlayerController : Script
if (rb != null) if (rb != null)
{ {
//check player vel.y if its close to zero its on the ground //check player vel.y if its close to zero its on the ground
if (SHADE.Math.CompareFloat(rb.LinearVelocity.y, 0.0f, 0.1f)) if (SHADE.Math.CompareFloat(rb.LinearVelocity.y, 0.0f))
isGrounded = true; isGrounded = true;
else else
isGrounded = false; isGrounded = false;
@ -224,8 +239,20 @@ public class PlayerController : Script
if (isGrounded) if (isGrounded)
v.y = groundGravity; v.y = groundGravity;
else if (currentState == RaccoonStates.FALLING)
{
float prevYVel = v.y;
float newYVel = v.y + (gravity * fallMultipler * (float)Time.DeltaTime);
float nextYVel = (prevYVel + newYVel) * 0.5f;
v.y = nextYVel;
}
else else
v.y += gravity * (float)Time.DeltaTime; {
float prevYVel = v.y;
float newYVel = v.y + (gravity * (float)Time.DeltaTime);
float nextYVel = (prevYVel + newYVel) * 0.5f;
v.y = nextYVel;
}
rb.LinearVelocity = v; rb.LinearVelocity = v;