Unity: Throwable Objects
Able to throw objects that have been picked up
This commit is contained in:
parent
3ffb61b5a9
commit
d68f3bd970
@ -5,9 +5,11 @@ using UnityEngine;
|
||||
public class Movement : MonoBehaviour
|
||||
{
|
||||
public float speed = 5.0f;
|
||||
public float turnSpeed;
|
||||
public float sprintSpeed = 10.0f;
|
||||
public float jumpForce;
|
||||
public float gravity = 9.8f;
|
||||
public PickUpObject throwForce;
|
||||
|
||||
private CharacterController controller;
|
||||
private Vector3 moveDirection = Vector3.zero;
|
||||
@ -21,13 +23,28 @@ public class Movement : MonoBehaviour
|
||||
{
|
||||
if (controller.isGrounded)
|
||||
{
|
||||
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
|
||||
moveDirection = transform.TransformDirection(moveDirection);
|
||||
// Change the direction the player is facing based on the direction they are moving
|
||||
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))
|
||||
{
|
||||
moveDirection *= sprintSpeed;
|
||||
jumpForce = 4.0f;
|
||||
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
|
||||
{
|
||||
throwForce.throwForce = 20.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
throwForce.throwForce = 5.0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
@ -9,6 +10,7 @@ public class PickUpObject : MonoBehaviour
|
||||
public bool insideTrigger = false;
|
||||
public bool carrying = false;
|
||||
public GameObject carriableObject;
|
||||
public float throwForce;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
@ -27,6 +29,8 @@ public class PickUpObject : MonoBehaviour
|
||||
{
|
||||
carrying = 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)
|
||||
@ -38,7 +42,7 @@ public class PickUpObject : MonoBehaviour
|
||||
|
||||
void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if(other.gameObject.tag == "PickupObject" && !carrying)
|
||||
if(other.gameObject.tag == "PickupObject")
|
||||
{
|
||||
carriableObject = other.gameObject;
|
||||
// If player presses D while inside the trigger, pick up the object
|
||||
|
@ -478,9 +478,11 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
speed: 5
|
||||
turnSpeed: 720
|
||||
sprintSpeed: 10
|
||||
jumpForce: 2.5
|
||||
gravity: 9.8
|
||||
throwForce: {fileID: 765999536}
|
||||
--- !u!114 &765999536
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
@ -497,6 +499,7 @@ MonoBehaviour:
|
||||
insideTrigger: 0
|
||||
carrying: 0
|
||||
carriableObject: {fileID: 0}
|
||||
throwForce: 5
|
||||
--- !u!1 &876529135
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@ -702,7 +705,7 @@ Transform:
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 963194225}
|
||||
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_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
|
Loading…
Reference in New Issue
Block a user