Add picking up mechanic

This commit is contained in:
Jens Pitkänen 2020-04-18 23:52:37 +03:00
parent 248fb742b0
commit d03bbf6f97
11 changed files with 334 additions and 5 deletions

View File

@ -0,0 +1,157 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &3482130534745816337
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 409532536335014064}
- component: {fileID: 2366637036822799078}
- component: {fileID: 7411394856680257321}
- component: {fileID: 5008951219788601564}
m_Layer: 0
m_Name: Cube
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &409532536335014064
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3482130534745816337}
m_LocalRotation: {x: 0, y: 0.7071068, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.7, y: 0.15, z: 0.15}
m_Children: []
m_Father: {fileID: 9177655811231306216}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 90, z: 0}
--- !u!33 &2366637036822799078
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3482130534745816337}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!23 &7411394856680257321
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3482130534745816337}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
--- !u!65 &5008951219788601564
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3482130534745816337}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!1 &9177655811231306215
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 9177655811231306216}
- component: {fileID: 456025085184268836}
- component: {fileID: 8167482766655706823}
m_Layer: 0
m_Name: Stick
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &9177655811231306216
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9177655811231306215}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 2.296, y: 2, z: 0.147}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 409532536335014064}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!54 &456025085184268836
Rigidbody:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9177655811231306215}
serializedVersion: 2
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_UseGravity: 1
m_IsKinematic: 0
m_Interpolate: 0
m_Constraints: 0
m_CollisionDetection: 0
--- !u!114 &8167482766655706823
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9177655811231306215}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f70b810850590804092bb2c772ea6012, type: 3}
m_Name:
m_EditorClassIdentifier:
Grabbed: 0

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 46cbe90ffaf140c4990d7e0232834b91
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,5 +1,35 @@
%YAML 1.1 %YAML 1.1
%TAG !u! tag:unity3d.com,2011: %TAG !u! tag:unity3d.com,2011:
--- !u!1 &4063427199230185944
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8433165236288212749}
m_Layer: 0
m_Name: Item Holder
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &8433165236288212749
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4063427199230185944}
m_LocalRotation: {x: -0.17101014, y: -0.17101014, z: -0.030153712, w: 0.96984625}
m_LocalPosition: {x: 0.3, y: -0.3, z: 1}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 6125707628839966534}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: -20, y: -20, z: 0}
--- !u!1 &6125707628839966531 --- !u!1 &6125707628839966531
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -29,7 +59,8 @@ Transform:
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 1.5, z: 0} m_LocalPosition: {x: 0, y: 1.5, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children:
- {fileID: 8433165236288212749}
m_Father: {fileID: 6125707630481988396} m_Father: {fileID: 6125707630481988396}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -156,6 +187,7 @@ GameObject:
- component: {fileID: 6125707630481988370} - component: {fileID: 6125707630481988370}
- component: {fileID: 6125707630481988397} - component: {fileID: 6125707630481988397}
- component: {fileID: 6125707630481988371} - component: {fileID: 6125707630481988371}
- component: {fileID: 7093134810128755097}
m_Layer: 0 m_Layer: 0
m_Name: Player m_Name: Player
m_TagString: Untagged m_TagString: Untagged
@ -230,3 +262,24 @@ CharacterController:
m_SkinWidth: 0.08 m_SkinWidth: 0.08
m_MinMoveDistance: 0.001 m_MinMoveDistance: 0.001
m_Center: {x: 0, y: 0.9, z: 0} m_Center: {x: 0, y: 0.9, z: 0}
--- !u!114 &7093134810128755097
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6125707630481988399}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8f679bace24150841865636642a63629, type: 3}
m_Name:
m_EditorClassIdentifier:
CameraTransform: {fileID: 6125707628839966534}
HandTransform: {fileID: 8433165236288212749}
GrabHint: {fileID: 0}
ItemLayer:
serializedVersion: 2
m_Bits: 1024
Distance: 2
ThrowVelocity: 6
GrabbedItem: {fileID: 0}

View File

@ -12,7 +12,7 @@ GameObject:
- component: {fileID: 5400695019851472734} - component: {fileID: 5400695019851472734}
- component: {fileID: 5400695019851472735} - component: {fileID: 5400695019851472735}
m_Layer: 5 m_Layer: 5
m_Name: Text m_Name: Pause Text
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0

37
Assets/Scripts/Item.cs Normal file
View File

@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Item : MonoBehaviour {
[Header("Runtime values")]
public bool Grabbed = false;
private Rigidbody Body;
private Transform World;
private void Awake() {
Body = GetComponent<Rigidbody>();
World = GameObject.FindGameObjectWithTag("World").transform;
}
private void Update() {
if (Grabbed) {
transform.localPosition = Vector3.Lerp(transform.localPosition, Vector3.zero, 10f * Time.deltaTime);
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, 10f * Time.deltaTime);
}
}
public void PickUp(Transform handTransform) {
transform.parent = handTransform;
Body.isKinematic = true;
Grabbed = true;
}
public void Drop(Vector3 throwVector) {
transform.parent = World;
Body.isKinematic = false;
Body.AddForce(throwVector, ForceMode.VelocityChange);
Grabbed = false;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f70b810850590804092bb2c772ea6012
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,51 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemGrabber : MonoBehaviour {
public Transform CameraTransform;
public Transform HandTransform;
public CanvasGroup GrabHint;
public LayerMask ItemLayer;
public float Distance;
public float ThrowVelocity;
[Header("Runtime values")]
public Item GrabbedItem;
private void Awake() {
if (GrabHint == null) {
Debug.LogWarning("Player's GrabHint is not set, and pick ups will not be indicated.");
}
}
private void Update() {
Item Item = null;
RaycastHit Hit;
Vector3 From = CameraTransform.position;
Vector3 Direction = CameraTransform.forward;
if (GrabbedItem == null) {
if (Physics.Raycast(From, Direction, out Hit, Distance, ItemLayer) &&
Hit.collider.attachedRigidbody != null) {
Item = Hit.collider.attachedRigidbody.GetComponent<Item>();
}
if (Item != null && Input.GetButtonDown("Grab")) {
Item.PickUp(HandTransform);
GrabbedItem = Item;
}
} else if (Input.GetButtonDown("Grab")) {
Vector3 Forward = CameraTransform.forward;
if (Physics.Raycast(From, Direction, out Hit, 10f)) {
// Throw direction should be where we're pointign, but slightly upwards
Forward = (Hit.point - From).normalized + Vector3.up;
}
GrabbedItem.Drop(Forward * ThrowVelocity);
GrabbedItem = null;
}
if (GrabHint != null) {
GrabHint.alpha = Mathf.Lerp(GrabHint.alpha, Item != null ? 1 : 0, 10f * Time.deltaTime);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8f679bace24150841865636642a63629
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -8,6 +8,7 @@ public class PauseMenu : MonoBehaviour {
private void Awake() { private void Awake() {
Canvas = GetComponent<CanvasGroup>(); Canvas = GetComponent<CanvasGroup>();
Canvas.alpha = Paused ? 1 : 0;
} }
private void Update() { private void Update() {

View File

@ -38,7 +38,7 @@ InputManager:
axis: 0 axis: 0
joyNum: 0 joyNum: 0
- serializedVersion: 3 - serializedVersion: 3
m_Name: Fire1 m_Name: Grab
descriptiveName: descriptiveName:
descriptiveNegativeName: descriptiveNegativeName:
negativeButton: negativeButton:
@ -182,7 +182,7 @@ InputManager:
axis: 1 axis: 1
joyNum: 0 joyNum: 0
- serializedVersion: 3 - serializedVersion: 3
m_Name: Fire1 m_Name: Grab
descriptiveName: descriptiveName:
descriptiveNegativeName: descriptiveNegativeName:
negativeButton: negativeButton:

View File

@ -5,6 +5,7 @@ TagManager:
serializedVersion: 2 serializedVersion: 2
tags: tags:
- Pause Menu - Pause Menu
- World
layers: layers:
- Default - Default
- TransparentFX - TransparentFX
@ -16,7 +17,7 @@ TagManager:
- -
- Post-Processing - Post-Processing
- Campfire - Campfire
- - Item
- -
- -
- -