Unity: Throwable Objects

Able to throw objects that have been picked up
This commit is contained in:
Santi 2024-04-28 17:14:08 +01:00
parent 3ffb61b5a9
commit d68f3bd970
3 changed files with 28 additions and 4 deletions

View File

@ -5,9 +5,11 @@ using UnityEngine;
public class Movement : MonoBehaviour public class Movement : MonoBehaviour
{ {
public float speed = 5.0f; public float speed = 5.0f;
public float turnSpeed;
public float sprintSpeed = 10.0f; public float sprintSpeed = 10.0f;
public float jumpForce; public float jumpForce;
public float gravity = 9.8f; public float gravity = 9.8f;
public PickUpObject throwForce;
private CharacterController controller; private CharacterController controller;
private Vector3 moveDirection = Vector3.zero; private Vector3 moveDirection = Vector3.zero;
@ -21,13 +23,28 @@ public class Movement : MonoBehaviour
{ {
if (controller.isGrounded) if (controller.isGrounded)
{ {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical")); // Change the direction the player is facing based on the direction they are moving
moveDirection = transform.TransformDirection(moveDirection); if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
Quaternion ToRotation = Quaternion.LookRotation(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")));
throwForce.throwForce = 7.5f;
transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime);
}
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (Input.GetKey(KeyCode.LeftShift)) if (Input.GetKey(KeyCode.LeftShift))
{ {
moveDirection *= sprintSpeed; moveDirection *= sprintSpeed;
jumpForce = 4.0f; jumpForce = 4.0f;
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
throwForce.throwForce = 20.0f;
}
else
{
throwForce.throwForce = 5.0f;
}
} }
else else
{ {

View File

@ -1,5 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
@ -9,6 +10,7 @@ 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 float throwForce;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
@ -27,6 +29,8 @@ public class PickUpObject : MonoBehaviour
{ {
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
carriableObject.GetComponent<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
} }
if(carrying) if(carrying)
@ -38,7 +42,7 @@ public class PickUpObject : MonoBehaviour
void OnTriggerEnter(Collider other) void OnTriggerEnter(Collider other)
{ {
if(other.gameObject.tag == "PickupObject" && !carrying) if(other.gameObject.tag == "PickupObject")
{ {
carriableObject = other.gameObject; carriableObject = other.gameObject;
// If player presses D while inside the trigger, pick up the object // If player presses D while inside the trigger, pick up the object

View File

@ -478,9 +478,11 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
speed: 5 speed: 5
turnSpeed: 720
sprintSpeed: 10 sprintSpeed: 10
jumpForce: 2.5 jumpForce: 2.5
gravity: 9.8 gravity: 9.8
throwForce: {fileID: 765999536}
--- !u!114 &765999536 --- !u!114 &765999536
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -497,6 +499,7 @@ MonoBehaviour:
insideTrigger: 0 insideTrigger: 0
carrying: 0 carrying: 0
carriableObject: {fileID: 0} carriableObject: {fileID: 0}
throwForce: 5
--- !u!1 &876529135 --- !u!1 &876529135
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -702,7 +705,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 963194225} m_GameObject: {fileID: 963194225}
m_LocalRotation: {x: 0.6870241, y: 0, z: 0, w: 0.7266347} m_LocalRotation: {x: 0.6870241, y: 0, z: 0, w: 0.7266347}
m_LocalPosition: {x: 0, y: 31.77, z: -1.56} m_LocalPosition: {x: 0, y: 35.9, z: -1.56}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []