Created
November 20, 2017 18:10
-
-
Save LeviVisser/07236b3a198ee14cf3987a4ac656dd01 to your computer and use it in GitHub Desktop.
Item interaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class InteractibleItem : MonoBehaviour | |
{ | |
public Rigidbody Rigidbody; | |
public BoxCollider collider; | |
private bool currentlyInteracting; | |
public WandController attachedHand; | |
private Transform interactionPoint; | |
public Transform specificInteractionoint; | |
public bool isToBeReleased; | |
public string itemTag; | |
// Use this for initialization | |
void Start() | |
{ | |
Rigidbody = this.GetComponent<Rigidbody>(); | |
collider = this.GetComponent<BoxCollider>(); | |
interactionPoint = new GameObject().transform; | |
} | |
// Update is called once per frame | |
void FixedUpdate() | |
{ | |
if (attachedHand && currentlyInteracting) | |
{ | |
float angle; | |
Vector3 axis; | |
Quaternion rotationDelta; | |
Vector3 posDelta; | |
collider.isTrigger = true; | |
if (specificInteractionoint != null) | |
{ | |
rotationDelta = attachedHand.transform.rotation * Quaternion.Inverse(specificInteractionoint.rotation); | |
posDelta = (attachedHand.transform.position - specificInteractionoint.position); | |
} | |
else | |
{ | |
rotationDelta = attachedHand.transform.rotation * Quaternion.Inverse(this.transform.rotation); | |
posDelta = (attachedHand.transform.position - this.transform.position); | |
} | |
rotationDelta.ToAngleAxis(out angle, out axis); | |
if (angle > 180) | |
{ | |
angle -= 360; | |
} | |
if (angle != 0) | |
{ | |
Vector3 AngularTarget = angle * axis; | |
this.Rigidbody.angularVelocity = Vector3.MoveTowards(this.Rigidbody.angularVelocity, AngularTarget, 10f); | |
} | |
Vector3 VelocityTarget = posDelta / Time.fixedDeltaTime; | |
this.Rigidbody.velocity = Vector3.MoveTowards(this.Rigidbody.velocity, VelocityTarget, 10f); | |
} | |
} | |
public void BeginInteraction(WandController wand) | |
{ | |
itemTag = this.gameObject.tag; | |
this.Rigidbody.isKinematic = false; | |
attachedHand = wand; | |
interactionPoint.position = wand.transform.position; | |
interactionPoint.rotation = wand.transform.rotation; | |
interactionPoint.SetParent(transform, true); | |
currentlyInteracting = true; | |
} | |
public void EndInteraction(WandController wand) | |
{ | |
if (wand == attachedHand && !isToBeReleased) | |
{ | |
itemTag = " "; | |
attachedHand = null; | |
currentlyInteracting = false; | |
collider.isTrigger = false; | |
} | |
} | |
public bool IsInteracting() | |
{ | |
return currentlyInteracting; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment