public bool TrySetThrowableObject(BaseGrabbable grabbable, InteractionSourcePose poseInfo) { if (grabbable == null) { return(false); } if (!grabbable.GetComponent <BaseThrowable>()) { return(false); } if (!grabbable.GetComponent <Rigidbody>()) { return(false); } Rigidbody rb = grabbable.GetComponent <Rigidbody>(); Debug.Log("name of our rb.center of mass ========= " + rb.name); ControllerReleaseData controlReleaseData = grabbable.GetComponent <Rigidbody>().GetThrowReleasedVelocityAndAngularVelocity(rb.centerOfMass, poseInfo); //grabbable.GetComponent<BaseThrowable>().LatestControllerThrowVelocity = vel; //grabbable.GetComponent<BaseThrowable>().LatestControllerThrowAngularVelocity = vel; grabbable.GetComponent <BaseThrowable>().LatestControllerThrowVelocity = controlReleaseData.Velocity; grabbable.GetComponent <BaseThrowable>().LatestControllerThrowAngularVelocity = controlReleaseData.AngleVelocity; return(true); }
/// <summary> /// Adding a grabber object to the list of scalars means adding it to the list of scalars and always attempting a scale if there are enough scalars attached /// </summary> /// <param Name="baseGrab"></param> public void OnGrabbed(BaseGrabbable baseGrab) { if (!currentlyScaling) { AttemptScale(); } }
protected virtual void Awake() { if (grabbable == null) { grabbable = gameObject.GetComponent <BaseGrabbable>(); } }
/// <summary> /// Adds a grabbable object to the list of available objects /// </summary> /// <param name="availableObject"></param> protected void AddContact(BaseGrabbable availableObject) { if (!contactObjects.Contains(availableObject)) { contactObjects.Add(availableObject); availableObject.AddContact(this); } }
/// <summary> /// For the demo only - if we throw an object, we respawn it at its initial location with the same throw properties as the previous one. /// This way a user can try out throw a few times /// </summary> /// <param name="baseGrab"></param> private void SpawnDuplicate(BaseGrabbable baseGrab) { GameObject thrown = Instantiate(ThrowObject, startPos, Quaternion.identity); thrown.GetComponent <ThrowableObject>().ZeroGravityThrow = GetComponent <BaseThrowable>().ZeroGravityThrow; thrown.GetComponent <ThrowableObject>().ThrowMultiplier = GetComponent <BaseThrowable>().ThrowMultiplier; thrown.GetComponent <Renderer>().material.color = startColor; thrown.GetComponent <Rigidbody>().useGravity = true; thrown.transform.rotation = startRot; }
/// <summary> /// Removes a grabbable object from the list of available objects /// </summary> /// <param name="availableObject"></param> protected void RemoveContact(BaseGrabbable availableObject) { contactObjects.Remove(availableObject); availableObject.RemoveContact(this); if (contactObjects.Contains(availableObject)) { // What's supposed to happen here? } }
private IEnumerator ThrowDelay(Vector3 vel, Vector3 angVel, BaseGrabbable grabbable) { yield return(null); GetComponent <Rigidbody>().velocity = vel * ThrowMultiplier; GetComponent <Rigidbody>().angularVelocity = angVel; if (ZeroGravityThrow) { grabbable.GetComponent <Rigidbody>().useGravity = false; } }
protected override void OnEnable() { base.OnEnable(); #if UNITY_WSA && UNITY_2017_2_OR_NEWER InteractionManager.InteractionSourceUpdated += GetTouchPadPosition; #endif if (baseGrabbable == null) { baseGrabbable = GetComponent <BaseGrabbable>(); } }
private void Awake() { if (grabbable == null) { grabbable = GetComponent <BaseGrabbable>(); } if (targetRenderer == null) { targetRenderer = gameObject.GetComponentInChildren <MeshRenderer>(); } originalColor = targetRenderer.material.color; grabbable.OnContactStateChange += RefreshColor; grabbable.OnGrabStateChange += RefreshColor; }
public override void Throw(BaseGrabbable grabbable) { base.Throw(grabbable); //Vector3 vel = grabbable.GetAverageVelocity(); Vector3 vel = LatestControllerThrowVelocity; Vector3 angVel = LatestControllerThrowAngularVelocity; if (GetComponent <GrabbableFixedJoint>() || GetComponent <GrabbableSpringJoint>()) { StartCoroutine(ThrowDelay(vel, angVel, grabbable)); } else { GetComponent <Rigidbody>().velocity = vel * ThrowMultiplier; GetComponent <Rigidbody>().angularVelocity = angVel; if (ZeroGravityThrow) { grabbable.GetComponent <Rigidbody>().useGravity = false; } } }
private void RefreshColor(BaseGrabbable baseGrab) { Color finalColor = originalColor; switch (baseGrab.ContactState) { case GrabStateEnum.Inactive: break; case GrabStateEnum.Multi: finalColor = colorOnContactMulti; break; case GrabStateEnum.Single: finalColor = colorOnContactSingle; break; default: throw new ArgumentOutOfRangeException(); } switch (baseGrab.GrabState) { case GrabStateEnum.Inactive: break; case GrabStateEnum.Multi: finalColor = colorOnGrabMulti; break; case GrabStateEnum.Single: finalColor = colorOnGrabSingle; break; default: throw new ArgumentOutOfRangeException(); } targetRenderer.material.color = finalColor; }
/// <summary> /// If the correct grabbing button is pressed, we add to grabbedObjects. /// </summary> protected virtual void GrabStart() { // Clean out the list of available objects list for (int i = contactObjects.Count - 1; i >= 0; i--) { if ((contactObjects[i] == null || !contactObjects[i].isActiveAndEnabled) && !grabbedObjects.Contains(contactObjects[i])) { contactObjects.RemoveAt(i); } } // If there are any left after pruning if (contactObjects.Count > 0) { // Sort by distance and try to grab the closest SortAvailable(); BaseGrabbable closestAvailable = contactObjects[0]; if (closestAvailable.TryGrabWith(this)) { grabbedObjects.Add(contactObjects[0]); } } }
protected virtual void OnTriggerExit(Collider other) { Debug.Log("Exited trigger with " + other.name); if (((1 << other.gameObject.layer) & grabbableLayers.value) == 0) { return; } BaseGrabbable bg = other.GetComponent <BaseGrabbable>(); if (bg == null && other.attachedRigidbody != null) { bg = other.attachedRigidbody.GetComponent <BaseGrabbable>(); } if (bg == null) { return; } Debug.Log("Removing contact"); RemoveContact(bg); }
/// <summary> /// Attempts to transfer ownership of grabbable object to another grabber /// Can override to 'lock' objects to a grabber, if desired /// </summary> /// <param name="ownerGrab"></param> /// <param name="otherGrabber"></param> /// <returns></returns> public virtual bool CanTransferOwnershipTo(BaseGrabbable ownerGrab, BaseGrabber otherGrabber) { Debug.Log("Transferring ownership of " + ownerGrab.name + " to grabber " + otherGrabber.name); grabbedObjects.Remove(ownerGrab); return(true); }
protected virtual void Awake() { grabbable = GetComponent <BaseGrabbable>(); }
/// <summary> /// throw behaviour should be over ridden in a non-abstract class /// </summary> /// <param name="grabber"></param> public virtual void Throw(BaseGrabbable grabber) { Debug.Log("Throwing.."); thrown = true; }
public bool IsGrabbing(BaseGrabbable grabbable) { return(grabbedObjects.Contains(grabbable)); }