Init
This commit is contained in:
13
Assets/Scripts/Helpers.cs
Normal file
13
Assets/Scripts/Helpers.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public static class GenericExtensions{
|
||||
public static Vector3 with (this Vector3 v, float? x = null, float? y=null, float? z=null){
|
||||
return new Vector3(x ?? v.x, y ?? v.y, z ?? v.z);
|
||||
}
|
||||
|
||||
public static Quaternion with (this Quaternion v, float? x = null, float? y=null, float? z=null, float? w = null){
|
||||
return new Quaternion(x ?? v.x, y ?? v.y, z ?? v.z, w ?? v.w);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Helpers.cs.meta
Normal file
11
Assets/Scripts/Helpers.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f1d4d1873620d5b40ad1749f1b7856dd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
71
Assets/Scripts/ItemGrabber.cs
Normal file
71
Assets/Scripts/ItemGrabber.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ItemGrabber : MonoBehaviour
|
||||
{
|
||||
public static ItemGrabber instance;
|
||||
void Awake()
|
||||
{
|
||||
instance = this;
|
||||
}
|
||||
public float pickupDistance;
|
||||
public Pickable currentTarget;
|
||||
public Pickable grabbingItem;
|
||||
public Transform holdingPoint;
|
||||
public Transform scanningItem;
|
||||
public Transform arm;
|
||||
void Update()
|
||||
{
|
||||
ScanForTarget();
|
||||
CalculateHoldingPointVelocity();
|
||||
|
||||
arm.transform.localRotation = arm.transform.localRotation.with(x: Camera.main.transform.localRotation.x);
|
||||
|
||||
if (currentTarget != null && Input.GetMouseButtonDown(0))
|
||||
{
|
||||
currentTarget.Grab();
|
||||
|
||||
grabbingItem = currentTarget;
|
||||
currentTarget = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (grabbingItem != null && Input.GetMouseButtonUp(0))
|
||||
{
|
||||
grabbingItem.LetGo(holdingPointVelocity);
|
||||
grabbingItem = null;
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 m_lastHoldingPointPos;
|
||||
public Vector3 holdingPointVelocity;
|
||||
void CalculateHoldingPointVelocity(){
|
||||
holdingPointVelocity = holdingPoint.position - m_lastHoldingPointPos;
|
||||
m_lastHoldingPointPos = holdingPoint.position;
|
||||
}
|
||||
void ScanForTarget()
|
||||
{
|
||||
if (grabbingItem != null) { return; }
|
||||
|
||||
RaycastHit hit = new RaycastHit();
|
||||
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
|
||||
if (Physics.Raycast(ray, out hit, pickupDistance))
|
||||
{
|
||||
scanningItem = hit.transform;
|
||||
Pickable pick = hit.transform.GetComponent<Pickable>();
|
||||
if (pick != null)
|
||||
{
|
||||
currentTarget = pick.isGrabbing ? null : pick;
|
||||
Debug.DrawRay(transform.position, hit.point, Color.green);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
currentTarget = null;
|
||||
Debug.DrawRay(transform.position, hit.point, Color.red);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ItemGrabber.cs.meta
Normal file
11
Assets/Scripts/ItemGrabber.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ffa64093d1c12e7478b8d44756d28d4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
47
Assets/Scripts/Pickable.cs
Normal file
47
Assets/Scripts/Pickable.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Pickable : MonoBehaviour
|
||||
{
|
||||
public Rigidbody rb;
|
||||
public Transform handlingPoint;
|
||||
public bool isGrabbing;
|
||||
|
||||
void OnValidate(){
|
||||
if(handlingPoint == null){
|
||||
handlingPoint = transform;
|
||||
}
|
||||
if(rb == null){
|
||||
rb = GetComponent<Rigidbody>();
|
||||
}
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
ogParent = transform.parent;
|
||||
|
||||
|
||||
}
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
Transform ogParent;
|
||||
|
||||
public void Grab(){
|
||||
rb.isKinematic=true;
|
||||
isGrabbing=true;
|
||||
transform.localRotation = new Quaternion(0,0,0,0);
|
||||
|
||||
transform.parent = ItemGrabber.instance.holdingPoint;
|
||||
transform.localPosition = transform.position - handlingPoint.position;
|
||||
}
|
||||
|
||||
public void LetGo(Vector3 vel){
|
||||
isGrabbing = false;
|
||||
transform.parent = ogParent;
|
||||
rb.isKinematic=false;
|
||||
rb.AddForce(vel * 100, ForceMode.VelocityChange);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Pickable.cs.meta
Normal file
11
Assets/Scripts/Pickable.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 573c8a578a868b14e95df9cc21c20a0b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user