From 94b71fc6101c085cf7e5c5362931f6fcfcb26d72 Mon Sep 17 00:00:00 2001 From: Santi Date: Sun, 28 Apr 2024 19:49:14 +0100 Subject: [PATCH] Unity: Thorwable objects with interact key --- Assets/Scenes/Character.unity | 11 +++++++++++ Assets/Scripts.meta | 8 ++++++++ Assets/Scripts/PickUpObject.cs | 20 +++++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 Assets/Scripts.meta diff --git a/Assets/Scenes/Character.unity b/Assets/Scenes/Character.unity index d12c958..0a4086c 100644 --- a/Assets/Scenes/Character.unity +++ b/Assets/Scenes/Character.unity @@ -483,6 +483,14 @@ MonoBehaviour: jumpForce: 2.5 gravity: 9.8 throwForce: {fileID: 765999536} + controller: {fileID: 0} + moveDirection: {x: 0, y: 0, z: 0} + forwardsKey: 119 + backwardsKey: 115 + leftKey: 97 + rightKey: 101 + sprintKey: 304 + jumpKey: 32 --- !u!114 &765999536 MonoBehaviour: m_ObjectHideFlags: 0 @@ -499,7 +507,10 @@ MonoBehaviour: insideTrigger: 0 carrying: 0 carriableObject: {fileID: 0} + previousObject: {fileID: 0} + tempObject: {fileID: 0} throwForce: 5 + interactKey: 102 --- !u!1 &876529135 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts.meta b/Assets/Scripts.meta new file mode 100644 index 0000000..22bb713 --- /dev/null +++ b/Assets/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: f461b3b4173c411449b3187f57a38c7e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PickUpObject.cs b/Assets/Scripts/PickUpObject.cs index dbea3ba..7551db5 100644 --- a/Assets/Scripts/PickUpObject.cs +++ b/Assets/Scripts/PickUpObject.cs @@ -1,7 +1,9 @@ +using System; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; +using UnityEngine.Animations; using UnityEngine.UIElements; public class PickUpObject : MonoBehaviour @@ -10,8 +12,9 @@ public class PickUpObject : MonoBehaviour public bool insideTrigger = false; public bool carrying = false; public GameObject carriableObject; + public GameObject previousObject; + public GameObject tempObject; public float throwForce; - public KeyCode interactKey = KeyCode.F; // Start is called before the first frame update void Start() { @@ -21,13 +24,24 @@ public class PickUpObject : MonoBehaviour // Update is called once per frame void Update() { - if(insideTrigger && Input.GetKeyDown(interactKey) && !carrying) + if(tempObject != carriableObject) + { + previousObject = tempObject; + tempObject = carriableObject; + previousObject.GetComponent().isKinematic = false; + } + if(insideTrigger && Input.GetKeyDown(KeyCode.F) && !carrying) { carrying = true; carriableObject.GetComponent().isKinematic = true; + if(tempObject == null) + { + tempObject = carriableObject; + } } - else if(carrying && Input.GetKeyDown(interactKey)) + else if(carrying && Input.GetKeyDown(KeyCode.F)) { + tempObject = carriableObject; carrying = false; carriableObject.GetComponent().isKinematic = false; // Drop the object in the direction the player is moving and add a force to it depending on speed