From 7a6474eaf80e749caea7a892b2e572a1e97178f2 Mon Sep 17 00:00:00 2001 From: Glence Date: Sun, 30 Oct 2022 23:59:35 +0800 Subject: [PATCH 1/9] base class for playercontroller --- TempScriptsFolder/PlayerController.cs | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 TempScriptsFolder/PlayerController.cs diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs new file mode 100644 index 00000000..2e0123eb --- /dev/null +++ b/TempScriptsFolder/PlayerController.cs @@ -0,0 +1,52 @@ +using SHADE; +using System; + + +public class PlayerController : Script +{ + private RigidBody rb; + public float xAxisMove; + public float zAxisMove; + public float force = 800.0f; + + public PlayerController(GameObject gameObj) : base(gameObj) { } + + protected override void awake() + { + rb = GetComponent(); + if (rb == null) + { + Debug.LogError("RigidBody is NULL!"); + } + } + + protected override void update() + { + Move(); + + //float x = xAxisMove * force * (float)Time.DeltaTime; + //Debug.Log(x.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 (rb != null) + rb.AddForce(new Vector3(xAxisMove * force * (float)Time.DeltaTime, 0.0f, zAxisMove * force * (float)Time.DeltaTime)); + } +} + -- 2.40.1 From 0ffd596734f4aa1960ee899fb0b21787c39e1d46 Mon Sep 17 00:00:00 2001 From: Glence Date: Mon, 31 Oct 2022 16:45:47 +0800 Subject: [PATCH 2/9] update player controller --- TempScriptsFolder/PlayerController.cs | 109 ++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 8 deletions(-) diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs index 2e0123eb..0eb94d13 100644 --- a/TempScriptsFolder/PlayerController.cs +++ b/TempScriptsFolder/PlayerController.cs @@ -1,13 +1,37 @@ 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; - public float xAxisMove; - public float zAxisMove; - public float force = 800.0f; + 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) { } @@ -18,14 +42,22 @@ public class PlayerController : Script { Debug.LogError("RigidBody is NULL!"); } + + RaccoonStates currentState = RaccoonStates.IDILE; } protected override void update() { - Move(); + currentState = RaccoonStates.IDILE; - //float x = xAxisMove * force * (float)Time.DeltaTime; - //Debug.Log(x.ToString()); + 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() @@ -45,8 +77,69 @@ public class PlayerController : Script else zAxisMove = 0; - if (rb != null) - rb.AddForce(new Vector3(xAxisMove * force * (float)Time.DeltaTime, 0.0f, zAxisMove * force * (float)Time.DeltaTime)); + 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.AddForce(new Vector3(moveForce * xAxisMove * (float)Time.DeltaTime, 0.0f, 0.0f)); + else + { + Vector3 v = rb.LinearVelocity; + v.x = maxMoveVel; + rb.LinearVelocity = v; + } + + if (rb.LinearVelocity.z <= maxMoveVel) + rb.AddForce(new Vector3(0.0f, 0.0f, moveForce * zAxisMove * (float)Time.DeltaTime)); + else + { + Vector3 v = rb.LinearVelocity; + 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 + + } + + } -- 2.40.1 From 48f01e41642ad8852ff2a6cecfc48aef73212f47 Mon Sep 17 00:00:00 2001 From: Glence Date: Mon, 31 Oct 2022 18:05:36 +0800 Subject: [PATCH 3/9] camp vel --- TempScriptsFolder/PlayerController.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs index 0eb94d13..29ac9007 100644 --- a/TempScriptsFolder/PlayerController.cs +++ b/TempScriptsFolder/PlayerController.cs @@ -82,21 +82,28 @@ public class PlayerController : Script if (rb != null && currentState == RaccoonStates.WALKING) { - if (rb.LinearVelocity.x <= maxMoveVel) - rb.AddForce(new Vector3(moveForce * xAxisMove * (float)Time.DeltaTime, 0.0f, 0.0f)); + if (rb.LinearVelocity.x <= maxMoveVel || rb.LinearVelocity.x >= -maxMoveVel) + rb.AddForce(new Vector3(moveForce * xAxisMove, 0.0f, 0.0f)); else { Vector3 v = rb.LinearVelocity; - v.x = maxMoveVel; + if(v.x >= 0) + v.x = maxMoveVel; + else + v.x = -maxMoveVel; + rb.LinearVelocity = v; } - if (rb.LinearVelocity.z <= maxMoveVel) - rb.AddForce(new Vector3(0.0f, 0.0f, moveForce * zAxisMove * (float)Time.DeltaTime)); + if (rb.LinearVelocity.z <= maxMoveVel || rb.LinearVelocity.z >= -maxMoveVel) + rb.AddForce(new Vector3(0.0f, 0.0f, moveForce * zAxisMove )); else { Vector3 v = rb.LinearVelocity; - v.z = maxMoveVel; + if (v.z >= 0) + v.z = maxMoveVel; + else + v.z = -maxMoveVel; rb.LinearVelocity = v; } } -- 2.40.1 From dc7d5a7ec30a2379f2af47316aaa0f731b4f8123 Mon Sep 17 00:00:00 2001 From: Glence Date: Tue, 1 Nov 2022 01:31:13 +0800 Subject: [PATCH 4/9] updates to the player controller --- TempScriptsFolder/CameraFix.cs | 24 ++++++ TempScriptsFolder/PlayerController.cs | 117 +++++++++++++++----------- 2 files changed, 92 insertions(+), 49 deletions(-) create mode 100644 TempScriptsFolder/CameraFix.cs diff --git a/TempScriptsFolder/CameraFix.cs b/TempScriptsFolder/CameraFix.cs new file mode 100644 index 00000000..5347a72f --- /dev/null +++ b/TempScriptsFolder/CameraFix.cs @@ -0,0 +1,24 @@ +using SHADE; +using System; + +public class CameraFix : Script +{ + public CameraFix(GameObject gameObj) : base(gameObj) { } + + private Transform tranform; + public Vector3 pos = Vector3.Zero; + public Vector3 rot = Vector3.Zero; + protected override void awake() + { + tranform = GetComponent(); + if (tranform == null) + Debug.LogError("tranform is NULL!"); + else + { + tranform.LocalPosition = pos; + tranform.LocalEulerAngles = rot; + } + + + } +} diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs index 29ac9007..4e28213d 100644 --- a/TempScriptsFolder/PlayerController.cs +++ b/TempScriptsFolder/PlayerController.cs @@ -18,20 +18,23 @@ public class PlayerController : Script } private RigidBody rb; - private float xAxisMove; - private float zAxisMove; - private RaccoonStates currentState = RaccoonStates.IDILE; + private Transform tranform; + public float xAxisMove; + public float zAxisMove; + private bool isMoveKeyPress = false; + public RaccoonStates currentState = RaccoonStates.IDILE; public float maxMoveVel = 2.0f; public float moveForce = 200.0f; public float sprintMultiplier = 1.5f; + public float rotationFactorPerFrame = 1.0f; public float jumpForce = 500.0f; - public float initialJumpForce = 100.0f; +/* public float initialJumpForce = 100.0f; public float maxJumpForce = 500.0f; public float maxJumpTime = 500.0f; private bool isJumping = false; - private bool isGrounded = false; + private bool isGrounded = false;*/ public PlayerController(GameObject gameObj) : base(gameObj) { } @@ -39,30 +42,38 @@ public class PlayerController : Script { rb = GetComponent(); if (rb == null) - { Debug.LogError("RigidBody is NULL!"); - } + + tranform = GetComponent(); + 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); RaccoonStates currentState = RaccoonStates.IDILE; } protected override void update() { - currentState = RaccoonStates.IDILE; + 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); + } + MoveKey(); + //Rotation(); Move(); Sprint(); - Jump(); + //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()); + Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString()); } - private void Move() + private void MoveKey() { - if (Input.GetKey(Input.KeyCode.A)) xAxisMove = -1; else if (Input.GetKey(Input.KeyCode.D)) @@ -77,57 +88,47 @@ public class PlayerController : Script 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) + isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; + + if(isMoveKeyPress && currentState != RaccoonStates.RUNNING) currentState = RaccoonStates.WALKING; - if (rb != null && currentState == RaccoonStates.WALKING) + if (!isMoveKeyPress) + currentState = RaccoonStates.IDILE; + } + + private void Move() + { + if (isMoveKeyPress && rb != null) { - if (rb.LinearVelocity.x <= maxMoveVel || rb.LinearVelocity.x >= -maxMoveVel) - rb.AddForce(new Vector3(moveForce * xAxisMove, 0.0f, 0.0f)); - else + rb.AddForce(new Vector3(moveForce * xAxisMove, 0.0f, moveForce * zAxisMove)); + + if (rb.LinearVelocity.x > maxMoveVel || rb.LinearVelocity.x < -maxMoveVel) { Vector3 v = rb.LinearVelocity; - if(v.x >= 0) - v.x = maxMoveVel; - else - v.x = -maxMoveVel; - + 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) - 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 (Input.GetKeyDown(Input.KeyCode.LeftShift) && currentState == RaccoonStates.WALKING && isMoveKeyPress) { - if (currentState == RaccoonStates.WALKING && rb != null) - { - currentState = RaccoonStates.RUNNING; - rb.LinearVelocity *= sprintMultiplier; - } + currentState = RaccoonStates.RUNNING; } - - if (Input.GetKeyUp(Input.KeyCode.RightShift)) + else if (Input.GetKeyUp(Input.KeyCode.LeftShift) && currentState == RaccoonStates.RUNNING && isMoveKeyPress) { - if (currentState == RaccoonStates.RUNNING && rb != null) - { - currentState = RaccoonStates.WALKING; - rb.LinearVelocity /= sprintMultiplier; - } + currentState = RaccoonStates.WALKING; } } @@ -147,6 +148,24 @@ public class PlayerController : Script } + 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); + } + } + } + } -- 2.40.1 From c4ed57fea8b16cf8dcc7a465a3b63f24fce96502 Mon Sep 17 00:00:00 2001 From: Glence Date: Tue, 1 Nov 2022 13:24:14 +0800 Subject: [PATCH 5/9] sprinting works now --- TempScriptsFolder/PlayerController.cs | 53 +++++++++++++++++++++------ 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs index 4e28213d..468ae162 100644 --- a/TempScriptsFolder/PlayerController.cs +++ b/TempScriptsFolder/PlayerController.cs @@ -19,22 +19,26 @@ public class PlayerController : Script private RigidBody rb; private Transform tranform; - public float xAxisMove; - public float zAxisMove; + private float xAxisMove; + private float zAxisMove; + public float drag = 4.0f; private bool isMoveKeyPress = false; public RaccoonStates currentState = RaccoonStates.IDILE; public float maxMoveVel = 2.0f; public float moveForce = 200.0f; public float sprintMultiplier = 1.5f; + private float oldForce; + private float maxOldVel; + private bool sprintIncreaseOnce = false; public float rotationFactorPerFrame = 1.0f; public float jumpForce = 500.0f; /* public float initialJumpForce = 100.0f; public float maxJumpForce = 500.0f; - public float maxJumpTime = 500.0f; + public float maxJumpTime = 500.0f;*/ private bool isJumping = false; - private bool isGrounded = false;*/ + private bool isGrounded = false; public PlayerController(GameObject gameObj) : base(gameObj) { } @@ -43,6 +47,8 @@ public class PlayerController : Script rb = GetComponent(); if (rb == null) Debug.LogError("RigidBody is NULL!"); + else + rb.Drag = drag; tranform = GetComponent(); if(tranform == null) @@ -50,8 +56,6 @@ public class PlayerController : Script tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); tranform.LocalRotation = Quaternion.Euler(0.0f,0.0f,0.0f); - - RaccoonStates currentState = RaccoonStates.IDILE; } protected override void update() @@ -66,12 +70,17 @@ public class PlayerController : Script //Rotation(); Move(); Sprint(); - //Jump(); + 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() + { + } + + private void MoveKey() { if (Input.GetKey(Input.KeyCode.A)) @@ -122,13 +131,27 @@ public class PlayerController : Script private void Sprint() { - if (Input.GetKeyDown(Input.KeyCode.LeftShift) && currentState == RaccoonStates.WALKING && isMoveKeyPress) + if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress) { currentState = RaccoonStates.RUNNING; + if(!sprintIncreaseOnce) + { + sprintIncreaseOnce = true; + oldForce = moveForce; + moveForce = moveForce * sprintMultiplier; + + maxOldVel = maxMoveVel; + maxMoveVel = maxMoveVel * sprintMultiplier; + } } - else if (Input.GetKeyUp(Input.KeyCode.LeftShift) && currentState == RaccoonStates.RUNNING && isMoveKeyPress) + + if (Input.GetKeyUp(Input.KeyCode.LeftShift) && (currentState == RaccoonStates.RUNNING || currentState == RaccoonStates.IDILE)) { - currentState = RaccoonStates.WALKING; + if(isMoveKeyPress) + currentState = RaccoonStates.WALKING; + sprintIncreaseOnce = false; + moveForce = oldForce; + maxMoveVel = maxOldVel; } } @@ -140,6 +163,7 @@ public class PlayerController : Script if (Input.GetKeyDown(Input.KeyCode.Space) && rb != null) { currentState = RaccoonStates.INAIR; + isJumping = true; rb.AddForce(new Vector3(0.0f, jumpForce, 0.0f)); } } @@ -166,6 +190,11 @@ public class PlayerController : Script } } - + protected override void onCollisionEnter(CollisionInfo info) + { + Debug.Log($"Collision Enter: {info.GameObject.Name}"); + } + + } -- 2.40.1 From e397d180dd3f065d98b1575ad012b40045de3065 Mon Sep 17 00:00:00 2001 From: Glence Date: Tue, 1 Nov 2022 17:49:01 +0800 Subject: [PATCH 6/9] jumping almost there --- TempScriptsFolder/PlayerController.cs | 113 ++++++++++++++++++-------- 1 file changed, 77 insertions(+), 36 deletions(-) diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs index 468ae162..98c3d3b5 100644 --- a/TempScriptsFolder/PlayerController.cs +++ b/TempScriptsFolder/PlayerController.cs @@ -19,47 +19,58 @@ public class PlayerController : Script private RigidBody rb; private Transform tranform; - private float xAxisMove; - private float zAxisMove; - public float drag = 4.0f; + private int xAxisMove; + private int zAxisMove; + public float drag = 2.0f; private bool isMoveKeyPress = false; public RaccoonStates currentState = RaccoonStates.IDILE; public float maxMoveVel = 2.0f; - public float moveForce = 200.0f; - public float sprintMultiplier = 1.5f; + public float moveForce = 50.0f; + public float sprintMultiplier = 2.0f; private float oldForce; private float maxOldVel; private bool sprintIncreaseOnce = false; public float rotationFactorPerFrame = 1.0f; - public float jumpForce = 500.0f; -/* public float initialJumpForce = 100.0f; - public float maxJumpForce = 500.0f; - public float maxJumpTime = 500.0f;*/ + + public float initialJumpVel; + public float maxJumpHeight = 1.0f; + public float maxJumpTime = 0.5f; 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) { } protected override void awake() { + //rigidbody check rb = GetComponent(); if (rb == null) Debug.LogError("RigidBody is NULL!"); else rb.Drag = drag; + //Transform check tranform = GetComponent(); if(tranform == 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.LocalRotation = Quaternion.Euler(0.0f,0.0f,0.0f); + tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f); } protected override void update() { + //toRemove if (Input.GetKey(Input.KeyCode.G)) { tranform.LocalRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f); @@ -67,17 +78,18 @@ public class PlayerController : Script } 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() { + //Rotation(); + Move(); + Sprint(); + Jump(); + Gravity(); } @@ -108,24 +120,25 @@ public class PlayerController : Script private void Move() { - if (isMoveKeyPress && rb != null) + if (rb != null) { 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; - v.x = System.Math.Clamp(v.x, -maxMoveVel, maxMoveVel); - rb.LinearVelocity = v; + if (rb.LinearVelocity.x > maxMoveVel || rb.LinearVelocity.x < -maxMoveVel) + { + 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; oldForce = moveForce; - moveForce = moveForce * sprintMultiplier; + moveForce *= sprintMultiplier; 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 (Input.GetKeyDown(Input.KeyCode.Space) && rb != null) + if (Input.GetKeyDown(Input.KeyCode.Space)&& isGrounded && rb != null) { - currentState = RaccoonStates.INAIR; 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() @@ -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) { - Debug.Log($"Collision Enter: {info.GameObject.Name}"); } -- 2.40.1 From 97a39d62c6ab55d5655ef4a0c4d06721b204c66b Mon Sep 17 00:00:00 2001 From: Glence Date: Tue, 1 Nov 2022 23:28:31 +0800 Subject: [PATCH 7/9] player controller done for now --- TempScriptsFolder/PlayerController.cs | 85 ++++++++++++++++++--------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs index 98c3d3b5..0d374bae 100644 --- a/TempScriptsFolder/PlayerController.cs +++ b/TempScriptsFolder/PlayerController.cs @@ -10,7 +10,7 @@ public class PlayerController : Script IDILE, WALKING, RUNNING, - INAIR, + JUMP, FALLING, HOLDING, CAUGHT, @@ -19,26 +19,47 @@ public class PlayerController : Script private RigidBody rb; private Transform tranform; - private int xAxisMove; - private int zAxisMove; + + //to be remove public float drag = 2.0f; - private bool isMoveKeyPress = false; + [SerializeField] + [Tooltip("The current state fo the raccoon")] public RaccoonStates currentState = RaccoonStates.IDILE; + //Movement variables============================================================ + [SerializeField] + [Tooltip("Max vel for walking")] public float maxMoveVel = 2.0f; + [SerializeField] + [Tooltip("how much force is apply for walking")] public float moveForce = 50.0f; + [SerializeField] + [Tooltip("increase the moveForce and maxMoveVel by its amt")] public float sprintMultiplier = 2.0f; + private float oldForce; private float maxOldVel; 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 initialJumpVel; - public float maxJumpHeight = 1.0f; - public float maxJumpTime = 0.5f; - private bool isJumping = false; - public bool isGrounded = true; + //Jumping vars================================================================== + [SerializeField] + [Tooltip("max height of the jump")] + public float maxJumpHeight = 4.0f; + [SerializeField] + [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 groundGravity = -0.5f; @@ -80,7 +101,7 @@ public class PlayerController : Script 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() @@ -111,10 +132,10 @@ public class PlayerController : Script isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; - if(isMoveKeyPress && currentState != RaccoonStates.RUNNING) + if(isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded) currentState = RaccoonStates.WALKING; - if (!isMoveKeyPress) + if (!isMoveKeyPress && isGrounded) currentState = RaccoonStates.IDILE; } @@ -144,7 +165,7 @@ public class PlayerController : Script private void Sprint() { - if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress) + if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded) { currentState = RaccoonStates.RUNNING; 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) currentState = RaccoonStates.WALKING; @@ -173,23 +194,17 @@ public class PlayerController : Script { 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; - v.y = initialJumpVel; + v.y = initialJumpVel * 0.5f; rb.LinearVelocity = v; } } - if (rb != null) - { - if (rb.LinearVelocity.y > 0 && isJumping) - currentState = RaccoonStates.INAIR; - else if(rb.LinearVelocity.y < 0 && isJumping) - currentState = RaccoonStates.FALLING; - - } + if(rb != null && !isGrounded && (rb.LinearVelocity.y < 0.0f || Input.GetKeyUp(Input.KeyCode.Space))) + currentState = RaccoonStates.FALLING; } private void Rotation() @@ -215,7 +230,7 @@ public class PlayerController : Script 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)) + if (SHADE.Math.CompareFloat(rb.LinearVelocity.y, 0.0f)) isGrounded = true; else isGrounded = false; @@ -224,8 +239,20 @@ public class PlayerController : Script if (isGrounded) v.y = groundGravity; - else - v.y += gravity * (float)Time.DeltaTime; + 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 + { + float prevYVel = v.y; + float newYVel = v.y + (gravity * (float)Time.DeltaTime); + float nextYVel = (prevYVel + newYVel) * 0.5f; + v.y = nextYVel; + } rb.LinearVelocity = v; -- 2.40.1 From 4eef6143dcfcb71ea6f38a7527e29bf139d99049 Mon Sep 17 00:00:00 2001 From: Glence Date: Wed, 2 Nov 2022 00:25:43 +0800 Subject: [PATCH 8/9] base for pick and throw --- TempScriptsFolder/PickAndThrow.cs | 30 +++++++++++++++++++++++++++ TempScriptsFolder/PlayerController.cs | 1 + 2 files changed, 31 insertions(+) create mode 100644 TempScriptsFolder/PickAndThrow.cs diff --git a/TempScriptsFolder/PickAndThrow.cs b/TempScriptsFolder/PickAndThrow.cs new file mode 100644 index 00000000..834a508c --- /dev/null +++ b/TempScriptsFolder/PickAndThrow.cs @@ -0,0 +1,30 @@ +using SHADE; +using System; +using static PlayerController; + +public class PickAndThrow : Script +{ + private PlayerController pc; + public uint itemEID; + Transform itemHoldLocation; + public PickAndThrow(GameObject gameObj) : base(gameObj) { } + protected override void awake() + { + pc = GetScript(); + itemHoldLocation = GetComponentInChildren(); + } + protected override void update() + { + if (pc != null && pc.holdItem) + { + } + } + protected override void onCollisionEnter(CollisionInfo info) + { + if (info.GameObject.Name == "item" && Input.GetKey(Input.KeyCode.E)) + { + pc.holdItem = true; + itemEID = info.GameObject.EntityId; + } + } +} \ No newline at end of file diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs index 0d374bae..36e19860 100644 --- a/TempScriptsFolder/PlayerController.cs +++ b/TempScriptsFolder/PlayerController.cs @@ -22,6 +22,7 @@ public class PlayerController : Script //to be remove public float drag = 2.0f; + public bool holdItem = false; [SerializeField] [Tooltip("The current state fo the raccoon")] public RaccoonStates currentState = RaccoonStates.IDILE; -- 2.40.1 From fc569736458292542cb8fe74d5ca812436442568 Mon Sep 17 00:00:00 2001 From: Glence Date: Wed, 2 Nov 2022 17:31:57 +0800 Subject: [PATCH 9/9] added getright in camera.cxx/.hxx PickandThrow is done adjusted thirdPersonCamera and PlayerController to work with each other --- .../Inspector/SHEditorComponentView.hpp | 4 + SHADE_Managed/src/Components/Camera.cxx | 9 ++ SHADE_Managed/src/Components/Camera.hxx | 2 + TempScriptsFolder/PickAndThrow.cs | 82 +++++++++++++++-- TempScriptsFolder/PlayerController.cs | 88 +++++++++++++++---- TempScriptsFolder/ThirdPersonCamera.cs | 40 +++++---- 6 files changed, 182 insertions(+), 43 deletions(-) diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index 4777fc6a..12893de6 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -244,9 +244,12 @@ namespace SHADE SHCollider* collider = &component->GetCollider(i); auto cursorPos = ImGui::GetCursorPos(); + //collider->IsTrigger + if (collider->GetType() == SHCollider::Type::BOX) { SHEditorWidgets::BeginPanel(std::format("{} Box Collider #{}", ICON_FA_CUBE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); + SHEditorWidgets::CheckBox("Is Trigger", [collider]() {return collider->IsTrigger(); }, [collider](bool const& value) {collider->SetIsTrigger(value); }, "Is Trigger"); auto box = reinterpret_cast(collider->GetShape()); SHEditorWidgets::DragVec3 ( @@ -257,6 +260,7 @@ namespace SHADE else if (collider->GetType() == SHCollider::Type::SPHERE) { SHEditorWidgets::BeginPanel(std::format("{} Sphere Collider #{}", ICON_MD_CIRCLE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); + SHEditorWidgets::CheckBox("Is Trigger", [collider]() {return collider->IsTrigger(); }, [collider](bool const& value) {collider->SetIsTrigger(value); }, "Is Trigger"); auto sphere = reinterpret_cast(collider->GetShape()); SHEditorWidgets::DragFloat ( diff --git a/SHADE_Managed/src/Components/Camera.cxx b/SHADE_Managed/src/Components/Camera.cxx index 5e570cc1..0d0dbced 100644 --- a/SHADE_Managed/src/Components/Camera.cxx +++ b/SHADE_Managed/src/Components/Camera.cxx @@ -123,5 +123,14 @@ namespace SHADE } + Vector3 Camera::GetRight() + { + auto system = SHSystemManager::GetSystem(); + SHVec3 forward, up, right; + system->GetCameraAxis(*GetNativeComponent(), forward, right, up); + return Convert::ToCLI(right); + + } + } \ No newline at end of file diff --git a/SHADE_Managed/src/Components/Camera.hxx b/SHADE_Managed/src/Components/Camera.hxx index 257bff11..c6afeb6d 100644 --- a/SHADE_Managed/src/Components/Camera.hxx +++ b/SHADE_Managed/src/Components/Camera.hxx @@ -66,5 +66,7 @@ namespace SHADE void SetMainCamera(); void LookAt(Vector3 targetPosition); Vector3 GetForward(); + Vector3 GetRight(); + }; } \ No newline at end of file diff --git a/TempScriptsFolder/PickAndThrow.cs b/TempScriptsFolder/PickAndThrow.cs index 834a508c..09252247 100644 --- a/TempScriptsFolder/PickAndThrow.cs +++ b/TempScriptsFolder/PickAndThrow.cs @@ -5,26 +5,94 @@ using static PlayerController; public class PickAndThrow : Script { private PlayerController pc; - public uint itemEID; - Transform itemHoldLocation; + public GameObject item; + public Vector3 throwForce = new Vector3(200.0f, 300.0f, 200.0f); + private Transform itemTransform; + private RigidBody itemRidibody; + private Transform raccoonHoldLocation; + private float lastXDir; + private float lastZDir; + private bool inRange = false; public PickAndThrow(GameObject gameObj) : base(gameObj) { } + protected override void awake() { pc = GetScript(); - itemHoldLocation = GetComponentInChildren(); + raccoonHoldLocation = GetComponentInChildren(); + if (raccoonHoldLocation == null) + Debug.Log("CHILD EMPTY"); + itemTransform = item.GetComponent(); + if (itemTransform == null) + Debug.Log("Item transform EMPTY"); + itemRidibody = item.GetComponent(); + if (itemRidibody == null) + Debug.Log("Item rb EMPTY"); } protected override void update() { - if (pc != null && pc.holdItem) + if (!pc.isMoveKeyPress) { + if (pc.xAxisMove != 0) + { + lastXDir = pc.xAxisMove; + lastZDir = 0.0f; + } + if (pc.zAxisMove != 0) + { + lastXDir = 0.0f; + lastZDir = pc.zAxisMove; + } } + else + { + lastXDir = pc.xAxisMove; + lastZDir = pc.zAxisMove; + } + + if (pc != null && inRange && !pc.holdItem && Input.GetKey(Input.KeyCode.E)) + pc.holdItem = true; + + if (pc != null && itemRidibody != null && itemTransform!= null && pc.holdItem) + { + itemTransform.LocalPosition = raccoonHoldLocation.GlobalPosition; + itemRidibody.IsGravityEnabled = false; + itemRidibody.LinearVelocity = Vector3.Zero; + itemRidibody.AngularVelocity = Vector3.Zero; + + if (Input.GetMouseButtonDown(Input.MouseCode.LeftButton)) + { + pc.holdItem = false; + itemRidibody.IsGravityEnabled = true; + itemRidibody.AddForce(new Vector3(throwForce.x * lastXDir, throwForce.y, throwForce.z * lastZDir)); + itemRidibody.LinearVelocity += pc.rb.LinearVelocity; + Debug.Log($"x: {itemRidibody.LinearVelocity.x} z: {itemRidibody.LinearVelocity.z}"); + } + } + else if(!pc.holdItem) + itemRidibody.IsGravityEnabled = true; } protected override void onCollisionEnter(CollisionInfo info) { - if (info.GameObject.Name == "item" && Input.GetKey(Input.KeyCode.E)) + } + protected override void onTriggerEnter(CollisionInfo info) + { + Debug.Log("ENTER"); + if (info.GameObject == item && !pc.holdItem) { - pc.holdItem = true; - itemEID = info.GameObject.EntityId; + inRange = true; } } + protected override void onTriggerStay(CollisionInfo info) + { + Debug.Log("STAY"); + } + protected override void onTriggerExit(CollisionInfo info) + { + Debug.Log("EXIT"); + if (info.GameObject == item && !pc.holdItem) + { + inRange = false; + } + } + } \ No newline at end of file diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs index 36e19860..5566c411 100644 --- a/TempScriptsFolder/PlayerController.cs +++ b/TempScriptsFolder/PlayerController.cs @@ -12,17 +12,17 @@ public class PlayerController : Script RUNNING, JUMP, FALLING, - HOLDING, CAUGHT, TOTAL } - private RigidBody rb; + public RigidBody rb; private Transform tranform; + private Camera cam; //to be remove public float drag = 2.0f; - public bool holdItem = false; + public bool holdItem { get; set; } [SerializeField] [Tooltip("The current state fo the raccoon")] public RaccoonStates currentState = RaccoonStates.IDILE; @@ -41,9 +41,11 @@ public class PlayerController : Script private float oldForce; private float maxOldVel; private bool sprintIncreaseOnce = false; - private int xAxisMove; - private int zAxisMove; - private bool isMoveKeyPress = false; + + public float xAxisMove { get; set; } + public float zAxisMove { get; set; } + + public bool isMoveKeyPress { get; set; } [SerializeField] [Tooltip("curr not working")] @@ -68,12 +70,21 @@ public class PlayerController : Script protected override void awake() { + + isMoveKeyPress = false; + holdItem = false; //rigidbody check rb = GetComponent(); if (rb == null) Debug.LogError("RigidBody is NULL!"); else + { + rb.IsGravityEnabled = false; + rb.FreezeRotationX = true; + rb.FreezeRotationY = true; + rb.FreezeRotationZ = true; rb.Drag = drag; + } //Transform check tranform = GetComponent(); @@ -92,6 +103,9 @@ public class PlayerController : Script protected override void update() { + if (cam == null) + cam = GetComponentInChildren(); + //toRemove if (Input.GetKey(Input.KeyCode.G)) { @@ -117,20 +131,55 @@ public class PlayerController : Script private void MoveKey() { - 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.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;*/ + + + xAxisMove = 0; + zAxisMove = 0; if (Input.GetKey(Input.KeyCode.W)) - zAxisMove = -1; - else if (Input.GetKey(Input.KeyCode.S)) - zAxisMove = 1; - else - zAxisMove = 0; - + { + Vector3 camerAixs = cam.GetForward(); + camerAixs.y = 0; + camerAixs.Normalise(); + xAxisMove = camerAixs.x; + zAxisMove = camerAixs.z; + } + if (Input.GetKey(Input.KeyCode.S)) + { + Vector3 camerAixs = cam.GetForward(); + camerAixs.y = 0; + camerAixs.Normalise(); + xAxisMove = -camerAixs.x; + zAxisMove = -camerAixs.z; + } + if (Input.GetKey(Input.KeyCode.A)) + { + Vector3 camerAixs = cam.GetRight(); + camerAixs.y = 0; + camerAixs.Normalise(); + xAxisMove = -camerAixs.x; + zAxisMove = -camerAixs.z; + } + if (Input.GetKey(Input.KeyCode.D)) + { + Vector3 camerAixs = cam.GetRight(); + camerAixs.y = 0; + camerAixs.Normalise(); + xAxisMove = camerAixs.x; + zAxisMove = camerAixs.z; + } isMoveKeyPress = xAxisMove != 0 || zAxisMove != 0; if(isMoveKeyPress && currentState != RaccoonStates.RUNNING && isGrounded) @@ -169,7 +218,8 @@ public class PlayerController : Script if (Input.GetKey(Input.KeyCode.LeftShift) && isMoveKeyPress && isGrounded) { currentState = RaccoonStates.RUNNING; - if(!sprintIncreaseOnce) + holdItem = false; + if (!sprintIncreaseOnce) { sprintIncreaseOnce = true; oldForce = moveForce; diff --git a/TempScriptsFolder/ThirdPersonCamera.cs b/TempScriptsFolder/ThirdPersonCamera.cs index e3b043bd..618c562f 100644 --- a/TempScriptsFolder/ThirdPersonCamera.cs +++ b/TempScriptsFolder/ThirdPersonCamera.cs @@ -19,10 +19,13 @@ namespace SHADE_Scripting protected override void awake() { + AddComponent(); + if(!GetComponent()) { AddComponent(); } + GetComponent().SetMainCamera(); if (!GetComponent()) { AddComponent(); @@ -30,26 +33,29 @@ namespace SHADE_Scripting GetComponent().ArmLength = armLength; } - protected override void update() - { - CameraArm arm = GetComponent(); - if(arm) + protected override void update() + { + if (Input.GetMouseButton(Input.MouseCode.RightButton)) + { + CameraArm arm = GetComponent(); + if (arm) + { + Vector2 vel = Input.GetMouseVelocity(); + arm.Pitch -= vel.y * turnSpeedPitch * Time.DeltaTimeF; + arm.Yaw += vel.x * turnSpeedYaw * Time.DeltaTimeF; + + if (arm.Pitch > pitchClamp) { - Vector2 vel = Input.GetMouseVelocity(); - arm.Pitch -= vel.y * turnSpeedPitch * Time.DeltaTimeF; - arm.Yaw += vel.x * turnSpeedYaw * Time.DeltaTimeF; - - if(arm.Pitch > pitchClamp) - { - arm.Pitch = pitchClamp; - } - else if(arm.Pitch < -pitchClamp) - { - arm.Pitch = -pitchClamp; - } - + arm.Pitch = pitchClamp; } + else if (arm.Pitch < 0) + { + arm.Pitch = 0; + } + + } } + } } } -- 2.40.1