Unity: Thorwable objects with interact key

This commit is contained in:
Santi 2024-04-28 19:49:14 +01:00
parent 080f9a3428
commit 94b71fc610
3 changed files with 36 additions and 3 deletions

View File

@ -483,6 +483,14 @@ MonoBehaviour:
jumpForce: 2.5 jumpForce: 2.5
gravity: 9.8 gravity: 9.8
throwForce: {fileID: 765999536} 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 --- !u!114 &765999536
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -499,7 +507,10 @@ MonoBehaviour:
insideTrigger: 0 insideTrigger: 0
carrying: 0 carrying: 0
carriableObject: {fileID: 0} carriableObject: {fileID: 0}
previousObject: {fileID: 0}
tempObject: {fileID: 0}
throwForce: 5 throwForce: 5
interactKey: 102
--- !u!1 &876529135 --- !u!1 &876529135
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

8
Assets/Scripts.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f461b3b4173c411449b3187f57a38c7e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +1,9 @@
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Unity.VisualScripting; using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.UIElements; using UnityEngine.UIElements;
public class PickUpObject : MonoBehaviour public class PickUpObject : MonoBehaviour
@ -10,8 +12,9 @@ public class PickUpObject : MonoBehaviour
public bool insideTrigger = false; public bool insideTrigger = false;
public bool carrying = false; public bool carrying = false;
public GameObject carriableObject; public GameObject carriableObject;
public GameObject previousObject;
public GameObject tempObject;
public float throwForce; public float throwForce;
public KeyCode interactKey = KeyCode.F;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
@ -21,13 +24,24 @@ public class PickUpObject : MonoBehaviour
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
if(insideTrigger && Input.GetKeyDown(interactKey) && !carrying) if(tempObject != carriableObject)
{
previousObject = tempObject;
tempObject = carriableObject;
previousObject.GetComponent<Rigidbody>().isKinematic = false;
}
if(insideTrigger && Input.GetKeyDown(KeyCode.F) && !carrying)
{ {
carrying = true; carrying = true;
carriableObject.GetComponent<Rigidbody>().isKinematic = true; carriableObject.GetComponent<Rigidbody>().isKinematic = true;
} if(tempObject == null)
else if(carrying && Input.GetKeyDown(interactKey))
{ {
tempObject = carriableObject;
}
}
else if(carrying && Input.GetKeyDown(KeyCode.F))
{
tempObject = carriableObject;
carrying = false; carrying = false;
carriableObject.GetComponent<Rigidbody>().isKinematic = false; carriableObject.GetComponent<Rigidbody>().isKinematic = false;
// Drop the object in the direction the player is moving and add a force to it depending on speed // Drop the object in the direction the player is moving and add a force to it depending on speed