From d6804c09f4762976f8d892f8f1d53c120e8cf44b Mon Sep 17 00:00:00 2001 From: Glence Date: Fri, 4 Nov 2022 17:31:53 +0800 Subject: [PATCH 1/2] fixed player throwing added item script player jump is now affected by item weight --- TempScriptsFolder/Item.cs | 18 ++++++++ TempScriptsFolder/PickAndThrow.cs | 61 +++++++++++++-------------- TempScriptsFolder/PlayerController.cs | 48 +++++++++++++++++---- 3 files changed, 87 insertions(+), 40 deletions(-) create mode 100644 TempScriptsFolder/Item.cs diff --git a/TempScriptsFolder/Item.cs b/TempScriptsFolder/Item.cs new file mode 100644 index 00000000..96ec092d --- /dev/null +++ b/TempScriptsFolder/Item.cs @@ -0,0 +1,18 @@ +using SHADE; +using System; +public class Item : Script +{ + public enum ItemCategory + { + LIGHT, + MEDIUM, + HEAVY + } + + public ItemCategory currCategory; + public Item(GameObject gameObj) : base(gameObj) { } + + protected override void awake() + { + } +} \ No newline at end of file diff --git a/TempScriptsFolder/PickAndThrow.cs b/TempScriptsFolder/PickAndThrow.cs index 80fcd61a..ed59d7e8 100644 --- a/TempScriptsFolder/PickAndThrow.cs +++ b/TempScriptsFolder/PickAndThrow.cs @@ -4,9 +4,10 @@ using static PlayerController; public class PickAndThrow : Script { - private PlayerController pc; - public GameObject item; public Vector3 throwForce = new Vector3(200.0f, 300.0f, 200.0f); + public GameObject item; + private PlayerController pc; + private Camera cam; private Transform itemTransform; private RigidBody itemRidibody; private Transform raccoonHoldLocation; @@ -23,40 +24,35 @@ public class PickAndThrow : Script Debug.Log("CHILD EMPTY"); else raccoonHoldLocation.LocalPosition = new Vector3(0.0f, 1.0f, 0.0f); - - 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.isMoveKeyPress) + if (cam == null) + cam = GetComponentInChildren(); + else if (cam != null) { - if (pc.xAxisMove != 0) - { - lastXDir = pc.xAxisMove; - lastZDir = 0.0f; - } - if (pc.zAxisMove != 0) - { - lastXDir = 0.0f; - lastZDir = pc.zAxisMove; - } + Vector3 camerAixs = cam.GetForward(); + camerAixs.y = 0; + camerAixs.Normalise(); + lastXDir = camerAixs.x; + lastZDir = camerAixs.z; } - else + + if (item.GetScript() != null && itemTransform == null && itemRidibody == null) { - lastXDir = pc.xAxisMove; - lastZDir = pc.zAxisMove; + itemTransform = item.GetComponent(); + if (itemTransform == null) + Debug.Log("Item transform EMPTY"); + + itemRidibody = item.GetComponent(); + if (itemRidibody == null) + Debug.Log("Item rb EMPTY"); } if (pc != null && inRange && !pc.holdItem && Input.GetKey(Input.KeyCode.E)) pc.holdItem = true; - if (pc != null && itemRidibody != null && itemTransform!= null && pc.holdItem) + if (pc != null && itemRidibody != null && itemTransform != null && pc.holdItem) { itemTransform.LocalPosition = raccoonHoldLocation.GlobalPosition; itemRidibody.IsGravityEnabled = false; @@ -66,13 +62,13 @@ public class PickAndThrow : Script if (Input.GetMouseButtonDown(Input.MouseCode.LeftButton)) { pc.holdItem = false; + inRange = 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) + else if(!pc.holdItem && itemRidibody != null) itemRidibody.IsGravityEnabled = true; } protected override void onCollisionEnter(CollisionInfo info) @@ -80,20 +76,21 @@ public class PickAndThrow : Script } protected override void onTriggerEnter(CollisionInfo info) { - Debug.Log("ENTER"); - if (info.GameObject == item && !pc.holdItem) + //Debug.Log("ENTER"); + if (info.GameObject.GetScript() != null && !pc.holdItem) { + item = info.GameObject; inRange = true; } } protected override void onTriggerStay(CollisionInfo info) { - Debug.Log("STAY"); + //Debug.Log("STAY"); } protected override void onTriggerExit(CollisionInfo info) { - Debug.Log("EXIT"); - if (info.GameObject == item && !pc.holdItem) + //Debug.Log("EXIT"); + if (info.GameObject.GetScript() != null && !pc.holdItem) { inRange = false; } diff --git a/TempScriptsFolder/PlayerController.cs b/TempScriptsFolder/PlayerController.cs index 77a4b9d3..bcdf24d1 100644 --- a/TempScriptsFolder/PlayerController.cs +++ b/TempScriptsFolder/PlayerController.cs @@ -1,7 +1,6 @@ using SHADE; using System; - -//in air controls? +using static Item; public class PlayerController : Script { @@ -19,6 +18,7 @@ public class PlayerController : Script public RigidBody rb; private Transform tranform; private Camera cam; + private PickAndThrow pat; //to be remove public float drag = 2.0f; @@ -68,13 +68,24 @@ public class PlayerController : Script private float gravity = -9.8f; private float groundGravity = -0.5f; + //ItemMultipler================================================================== + public float lightMultiper = 0.75f; + public float mediumMultiper = 0.5f; + public float heavyMultiper = 0.25f; + public PlayerController(GameObject gameObj) : base(gameObj) { } protected override void awake() { - + //default setup isMoveKeyPress = false; holdItem = false; + + //Jump setup + float timeToApex = maxJumpTime / 2; + gravity = (-2 * maxJumpHeight) / MathF.Pow(timeToApex, 2); + initialJumpVel = (2 * maxJumpHeight) / timeToApex; + //rigidbody check rb = GetComponent(); if (rb == null) @@ -94,10 +105,10 @@ public class PlayerController : Script 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; + //PickAndThrow checl + pat = GetScript(); + if (pat == null) + Debug.LogError("PickAndThrow is NULL!"); //toRemove tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); @@ -116,9 +127,11 @@ public class PlayerController : Script tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); } + GotCaught(); MoveKey(); + //Debug.Log(currentState.ToString() + " x:" + rb.LinearVelocity.x.ToString() + " y:" + rb.LinearVelocity.y.ToString() + " z:" + rb.LinearVelocity.z.ToString()); } @@ -202,7 +215,7 @@ public class PlayerController : Script { if (rb != null) { - rb.AddForce(new Vector3(moveForce * axisMove.x, 0.0f, moveForce * axisMove.y)); + rb.AddForce(new Vector3(axisMove.x, 0.0f,axisMove.y) * moveForce); if (isMoveKeyPress) { @@ -259,6 +272,16 @@ public class PlayerController : Script currentState = RaccoonStates.JUMP; Vector3 v = rb.LinearVelocity; v.y = initialJumpVel * 0.5f; + if (pat != null && pat.item.GetScript() != null && holdItem) + { + Item item = pat.item.GetScript(); + if (item.currCategory == ItemCategory.LIGHT) + v.y *= lightMultiper; + if (item.currCategory == ItemCategory.MEDIUM) + v.y *= mediumMultiper; + if (item.currCategory == ItemCategory.HEAVY) + v.y *= heavyMultiper; + } rb.LinearVelocity = v; } } @@ -319,6 +342,15 @@ public class PlayerController : Script } } + private void GotCaught() + { + if (currentState == RaccoonStates.CAUGHT && tranform != null) + { + currentState = RaccoonStates.IDILE; + tranform.LocalPosition = new Vector3(-3.0f, -2.0f, -5.0f); + } + } + protected override void onCollisionEnter(CollisionInfo info) { } From be16fbed19c4aeeb6c8a4b67e4c4d32ea35debe2 Mon Sep 17 00:00:00 2001 From: Glence Date: Fri, 4 Nov 2022 17:35:09 +0800 Subject: [PATCH 2/2] added item script in the scene --- SHADE_Application/src/Scenes/SBTestScene.cpp | 1 + TempScriptsFolder/PickAndThrow.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index fa5d7868..7d3ca4ff 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -182,6 +182,7 @@ namespace Sandbox scriptEngine->AddScript(racoon, "PickAndThrow"); scriptEngine->AddScript(racoonCamera, "ThirdPersonCamera"); scriptEngine->AddScript(AI, "AIPrototype"); + scriptEngine->AddScript(item, "Item"); auto raccoonShowcase = SHEntityManager::CreateEntity(); auto& renderableShowcase = *SHComponentManager::GetComponent_s(raccoonShowcase); diff --git a/TempScriptsFolder/PickAndThrow.cs b/TempScriptsFolder/PickAndThrow.cs index ed59d7e8..ea814b36 100644 --- a/TempScriptsFolder/PickAndThrow.cs +++ b/TempScriptsFolder/PickAndThrow.cs @@ -4,7 +4,7 @@ using static PlayerController; public class PickAndThrow : Script { - public Vector3 throwForce = new Vector3(200.0f, 300.0f, 200.0f); + public Vector3 throwForce = new Vector3(100.0f, 200.0f, 100.0f); public GameObject item; private PlayerController pc; private Camera cam;