PlayerController and PickAndThrow #167
|
@ -19,47 +19,58 @@ public class PlayerController : Script
|
||||||
|
|
||||||
private RigidBody rb;
|
private RigidBody rb;
|
||||||
private Transform tranform;
|
private Transform tranform;
|
||||||
private float xAxisMove;
|
private int xAxisMove;
|
||||||
private float zAxisMove;
|
private int zAxisMove;
|
||||||
public float drag = 4.0f;
|
public float drag = 2.0f;
|
||||||
private bool isMoveKeyPress = false;
|
private bool isMoveKeyPress = false;
|
||||||
public RaccoonStates currentState = RaccoonStates.IDILE;
|
public RaccoonStates currentState = RaccoonStates.IDILE;
|
||||||
|
|
||||||
public float maxMoveVel = 2.0f;
|
public float maxMoveVel = 2.0f;
|
||||||
public float moveForce = 200.0f;
|
public float moveForce = 50.0f;
|
||||||
public float sprintMultiplier = 1.5f;
|
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;
|
||||||
public float rotationFactorPerFrame = 1.0f;
|
public float rotationFactorPerFrame = 1.0f;
|
||||||
|
|
||||||
public float jumpForce = 500.0f;
|
|
||||||
/* public float initialJumpForce = 100.0f;
|
public float initialJumpVel;
|
||||||
public float maxJumpForce = 500.0f;
|
public float maxJumpHeight = 1.0f;
|
||||||
public float maxJumpTime = 500.0f;*/
|
public float maxJumpTime = 0.5f;
|
||||||
private bool isJumping = false;
|
private bool isJumping = false;
|
||||||
private bool isGrounded = false;
|
public bool isGrounded = true;
|
||||||
|
private float gravity = -9.8f;
|
||||||
|
private float groundGravity = -0.5f;
|
||||||
|
|
||||||
public PlayerController(GameObject gameObj) : base(gameObj) { }
|
public PlayerController(GameObject gameObj) : base(gameObj) { }
|
||||||
|
|
||||||
protected override void awake()
|
protected override void awake()
|
||||||
{
|
{
|
||||||
|
//rigidbody check
|
||||||
rb = GetComponent<RigidBody>();
|
rb = GetComponent<RigidBody>();
|
||||||
if (rb == null)
|
if (rb == null)
|
||||||
Debug.LogError("RigidBody is NULL!");
|
Debug.LogError("RigidBody is NULL!");
|
||||||
else
|
else
|
||||||
rb.Drag = drag;
|
rb.Drag = drag;
|
||||||
|
|
||||||
|
//Transform check
|
||||||
tranform = GetComponent<Transform>();
|
tranform = GetComponent<Transform>();
|
||||||
if(tranform == null)
|
if(tranform == null)
|
||||||
Debug.LogError("tranform is NULL!");
|
Debug.LogError("tranform is NULL!");
|
||||||
|
|
||||||
|
//Jump setup
|
||||||
|
float timeToApex = maxJumpTime / 2;
|
||||||
|
gravity = (-2 * maxJumpHeight) / MathF.Pow(timeToApex, 2);
|
||||||
|
initialJumpVel = (2 * maxJumpHeight) / timeToApex;
|
||||||
|
|
||||||
|
//toRemove
|
||||||
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
|
tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f);
|
||||||
tranform.LocalRotation = Quaternion.Euler(0.0f,0.0f,0.0f);
|
tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void update()
|
protected override void update()
|
||||||
{
|
{
|
||||||
|
//toRemove
|
||||||
if (Input.GetKey(Input.KeyCode.G))
|
if (Input.GetKey(Input.KeyCode.G))
|
||||||
{
|
{
|
||||||
tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
|
tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
|
||||||
|
@ -67,17 +78,18 @@ public class PlayerController : Script
|
||||||
}
|
}
|
||||||
|
|
||||||
MoveKey();
|
MoveKey();
|
||||||
//Rotation();
|
|
||||||
Move();
|
|
||||||
Sprint();
|
|
||||||
Jump();
|
|
||||||
|
|
||||||
|
|
||||||
//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()
|
||||||
{
|
{
|
||||||
|
//Rotation();
|
||||||
|
Move();
|
||||||
|
Sprint();
|
||||||
|
Jump();
|
||||||
|
Gravity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -108,24 +120,25 @@ public class PlayerController : Script
|
||||||
|
|
||||||
private void Move()
|
private void Move()
|
||||||
{
|
{
|
||||||
if (isMoveKeyPress && rb != null)
|
if (rb != null)
|
||||||
{
|
{
|
||||||
rb.AddForce(new Vector3(moveForce * xAxisMove, 0.0f, moveForce * zAxisMove));
|
rb.AddForce(new Vector3(moveForce * xAxisMove, 0.0f, moveForce * zAxisMove));
|
||||||
|
|
||||||
if (rb.LinearVelocity.x > maxMoveVel || rb.LinearVelocity.x < -maxMoveVel)
|
if (isMoveKeyPress)
|
||||||
{
|
{
|
||||||
Vector3 v = rb.LinearVelocity;
|
if (rb.LinearVelocity.x > maxMoveVel || rb.LinearVelocity.x < -maxMoveVel)
|
||||||
v.x = System.Math.Clamp(v.x, -maxMoveVel, maxMoveVel);
|
{
|
||||||
rb.LinearVelocity = v;
|
Vector3 v = rb.LinearVelocity;
|
||||||
|
v.x = System.Math.Clamp(v.x, -maxMoveVel, maxMoveVel);
|
||||||
|
rb.LinearVelocity = v;
|
||||||
|
}
|
||||||
|
if (rb.LinearVelocity.z > maxMoveVel || rb.LinearVelocity.z < -maxMoveVel)
|
||||||
|
{
|
||||||
|
Vector3 v = rb.LinearVelocity;
|
||||||
|
v.z = System.Math.Clamp(v.z, -maxMoveVel, maxMoveVel);
|
||||||
|
rb.LinearVelocity = v;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (rb.LinearVelocity.z > maxMoveVel || rb.LinearVelocity.z < -maxMoveVel)
|
|
||||||
{
|
|
||||||
Vector3 v = rb.LinearVelocity;
|
|
||||||
v.z = System.Math.Clamp(v.z, -maxMoveVel, maxMoveVel);
|
|
||||||
rb.LinearVelocity = v;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,10 +151,10 @@ public class PlayerController : Script
|
||||||
{
|
{
|
||||||
sprintIncreaseOnce = true;
|
sprintIncreaseOnce = true;
|
||||||
oldForce = moveForce;
|
oldForce = moveForce;
|
||||||
moveForce = moveForce * sprintMultiplier;
|
moveForce *= sprintMultiplier;
|
||||||
|
|
||||||
maxOldVel = maxMoveVel;
|
maxOldVel = maxMoveVel;
|
||||||
maxMoveVel = maxMoveVel * sprintMultiplier;
|
maxMoveVel *= sprintMultiplier;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,16 +173,23 @@ 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) && rb != null)
|
if (Input.GetKeyDown(Input.KeyCode.Space)&& isGrounded && rb != null)
|
||||||
{
|
{
|
||||||
currentState = RaccoonStates.INAIR;
|
|
||||||
isJumping = true;
|
isJumping = true;
|
||||||
rb.AddForce(new Vector3(0.0f, jumpForce, 0.0f));
|
Vector3 v = rb.LinearVelocity;
|
||||||
|
v.y = initialJumpVel;
|
||||||
|
rb.LinearVelocity = v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//collision check when grounded
|
if (rb != null)
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
@ -190,9 +210,30 @@ public class PlayerController : Script
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Gravity()
|
||||||
|
{
|
||||||
|
if (rb != null)
|
||||||
|
{
|
||||||
|
//check player vel.y if its close to zero its on the ground
|
||||||
|
if (SHADE.Math.CompareFloat(rb.LinearVelocity.y, 0.0f, 0.1f))
|
||||||
|
isGrounded = true;
|
||||||
|
else
|
||||||
|
isGrounded = false;
|
||||||
|
|
||||||
|
Vector3 v = rb.LinearVelocity;
|
||||||
|
|
||||||
|
if (isGrounded)
|
||||||
|
v.y = groundGravity;
|
||||||
|
else
|
||||||
|
v.y += gravity * (float)Time.DeltaTime;
|
||||||
|
|
||||||
|
rb.LinearVelocity = v;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected override void onCollisionEnter(CollisionInfo info)
|
protected override void onCollisionEnter(CollisionInfo info)
|
||||||
{
|
{
|
||||||
Debug.Log($"Collision Enter: {info.GameObject.Name}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue