playMaker

Author Topic: Anyone know how to make an action for this script?  (Read 271 times)

beowulfkaine

  • Playmaker Newbie
  • *
  • Posts: 44
    • View Profile
Anyone know how to make an action for this script?
« on: July 23, 2017, 04:13:04 PM »
Hello!

Ok, I have this script (below) and I wanted to know if anyone knew how to make this into an action that "happens" when an fsm is activated or event triggered(more likely)?  Currently, the controller has to be over the object in order for this to work and the trigger pressed or not pressed, but it works on a collider system. I would like instead of physically being over the object to be activated by trigger within playmaker.  Is this possible? I can't read c# very well or enough really to understand how this script works. Any help would be great!

Code: [Select]
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: Handles the spawning and returning of the ItemPackage
//
//=============================================================================

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine.Events;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Valve.VR.InteractionSystem
{
//-------------------------------------------------------------------------
[RequireComponent( typeof( Interactable ) )]
public class ItemPackageSpawner : MonoBehaviour
{
public ItemPackage itemPackage
{
get
{
return _itemPackage;
}
set
{
CreatePreviewObject();
}
}

public ItemPackage _itemPackage;

private bool useItemPackagePreview = true;
private bool useFadedPreview = false;
private GameObject previewObject;

public bool requireTriggerPressToTake = false;
public bool requireTriggerPressToReturn = false;
public bool showTriggerHint = false;

[EnumFlags]
public Hand.AttachmentFlags attachmentFlags = Hand.defaultAttachmentFlags;
public string attachmentPoint;

public bool takeBackItem = false; // if a hand enters this trigger and has the item this spawner dispenses at the top of the stack, remove it from the stack

public bool acceptDifferentItems = false;

private GameObject spawnedItem;
private bool itemIsSpawned = false;

public UnityEvent pickupEvent;
public UnityEvent dropEvent;

public bool justPickedUpItem = false;


//-------------------------------------------------
private void CreatePreviewObject()
{
if ( !useItemPackagePreview )
{
return;
}

ClearPreview();

if ( useItemPackagePreview )
{
if ( itemPackage == null )
{
return;
}

if ( useFadedPreview == false ) // if we don't have a spawned item out there, use the regular preview
{
if ( itemPackage.previewPrefab != null )
{
previewObject = Instantiate( itemPackage.previewPrefab, transform.position, Quaternion.identity ) as GameObject;
previewObject.transform.parent = transform;
previewObject.transform.localRotation = Quaternion.identity;
}
}
else // there's a spawned item out there. Use the faded preview
{
if ( itemPackage.fadedPreviewPrefab != null )
{
previewObject = Instantiate( itemPackage.fadedPreviewPrefab, transform.position, Quaternion.identity ) as GameObject;
previewObject.transform.parent = transform;
previewObject.transform.localRotation = Quaternion.identity;
}
}
}
}


//-------------------------------------------------
void Start()
{
VerifyItemPackage();
}


//-------------------------------------------------
private void VerifyItemPackage()
{
if ( itemPackage == null )
{
ItemPackageNotValid();
}

if ( itemPackage.itemPrefab == null )
{
ItemPackageNotValid();
}
}


//-------------------------------------------------
private void ItemPackageNotValid()
{
Debug.LogError( "ItemPackage assigned to " + gameObject.name + " is not valid. Destroying this game object." );
Destroy( gameObject );
}


//-------------------------------------------------
private void ClearPreview()
{
foreach ( Transform child in transform )
{
if ( Time.time > 0 )
{
GameObject.Destroy( child.gameObject );
}
else
{
GameObject.DestroyImmediate( child.gameObject );
}
}
}


//-------------------------------------------------
void Update()
{
if ( ( itemIsSpawned == true ) && ( spawnedItem == null ) )
{
itemIsSpawned = false;
useFadedPreview = false;
dropEvent.Invoke();
CreatePreviewObject();
}
}


//-------------------------------------------------
private void OnHandHoverBegin( Hand hand )
{
ItemPackage currentAttachedItemPackage = GetAttachedItemPackage( hand );

if ( currentAttachedItemPackage == itemPackage ) // the item at the top of the hand's stack has an associated ItemPackage
{
if ( takeBackItem && !requireTriggerPressToReturn ) // if we want to take back matching items and aren't waiting for a trigger press
{
TakeBackItem( hand );
}
}

if ( !requireTriggerPressToTake ) // we don't require trigger press for pickup. Spawn and attach object.
{
SpawnAndAttachObject( hand );
}

if ( requireTriggerPressToTake && showTriggerHint )
{
ControllerButtonHints.ShowTextHint( hand, Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger, "PickUp" );
}
}


//-------------------------------------------------
private void TakeBackItem( Hand hand )
{
RemoveMatchingItemsFromHandStack( itemPackage, hand );

if ( itemPackage.packageType == ItemPackage.ItemPackageType.TwoHanded )
{
RemoveMatchingItemsFromHandStack( itemPackage, hand.otherHand );
}
}


//-------------------------------------------------
private ItemPackage GetAttachedItemPackage( Hand hand )
{
GameObject currentAttachedObject = hand.currentAttachedObject;

if ( currentAttachedObject == null ) // verify the hand is holding something
{
return null;
}

ItemPackageReference packageReference = hand.currentAttachedObject.GetComponent<ItemPackageReference>();
if ( packageReference == null ) // verify the item in the hand is matchable
{
return null;
}

ItemPackage attachedItemPackage = packageReference.itemPackage; // return the ItemPackage reference we find.

return attachedItemPackage;
}


//-------------------------------------------------
private void HandHoverUpdate( Hand hand )
{
if ( requireTriggerPressToTake )
{
if ( hand.controller != null && hand.controller.GetHairTriggerDown() )
{
SpawnAndAttachObject( hand );
}
}
}


//-------------------------------------------------
private void OnHandHoverEnd( Hand hand )
{
if ( !justPickedUpItem && requireTriggerPressToTake && showTriggerHint )
{
ControllerButtonHints.HideTextHint( hand, Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger );
}

justPickedUpItem = false;
}


//-------------------------------------------------
private void RemoveMatchingItemsFromHandStack( ItemPackage package, Hand hand )
{
for ( int i = 0; i < hand.AttachedObjects.Count; i++ )
{
ItemPackageReference packageReference = hand.AttachedObjects[i].attachedObject.GetComponent<ItemPackageReference>();
if ( packageReference != null )
{
ItemPackage attachedObjectItemPackage = packageReference.itemPackage;
if ( ( attachedObjectItemPackage != null ) && ( attachedObjectItemPackage == package ) )
{
GameObject detachedItem = hand.AttachedObjects[i].attachedObject;
hand.DetachObject( detachedItem );
}
}
}
}


//-------------------------------------------------
private void RemoveMatchingItemTypesFromHand( ItemPackage.ItemPackageType packageType, Hand hand )
{
for ( int i = 0; i < hand.AttachedObjects.Count; i++ )
{
ItemPackageReference packageReference = hand.AttachedObjects[i].attachedObject.GetComponent<ItemPackageReference>();
if ( packageReference != null )
{
if ( packageReference.itemPackage.packageType == packageType )
{
GameObject detachedItem = hand.AttachedObjects[i].attachedObject;
hand.DetachObject( detachedItem );
}
}
}
}


//-------------------------------------------------
private void SpawnAndAttachObject( Hand hand )
{
if ( hand.otherHand != null )
{
//If the other hand has this item package, take it back from the other hand
ItemPackage otherHandItemPackage = GetAttachedItemPackage( hand.otherHand );
if ( otherHandItemPackage == itemPackage )
{
TakeBackItem( hand.otherHand );
}
}

if ( showTriggerHint )
{
ControllerButtonHints.HideTextHint( hand, Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger );
}

if ( itemPackage.otherHandItemPrefab != null )
{
if ( hand.otherHand.hoverLocked )
{
//Debug.Log( "Not attaching objects because other hand is hoverlocked and we can't deliver both items." );
return;
}
}

// if we're trying to spawn a one-handed item, remove one and two-handed items from this hand and two-handed items from both hands
if ( itemPackage.packageType == ItemPackage.ItemPackageType.OneHanded )
{
RemoveMatchingItemTypesFromHand( ItemPackage.ItemPackageType.OneHanded, hand );
RemoveMatchingItemTypesFromHand( ItemPackage.ItemPackageType.TwoHanded, hand );
RemoveMatchingItemTypesFromHand( ItemPackage.ItemPackageType.TwoHanded, hand.otherHand );
}

// if we're trying to spawn a two-handed item, remove one and two-handed items from both hands
if ( itemPackage.packageType == ItemPackage.ItemPackageType.TwoHanded )
{
RemoveMatchingItemTypesFromHand( ItemPackage.ItemPackageType.OneHanded, hand );
RemoveMatchingItemTypesFromHand( ItemPackage.ItemPackageType.OneHanded, hand.otherHand );
RemoveMatchingItemTypesFromHand( ItemPackage.ItemPackageType.TwoHanded, hand );
RemoveMatchingItemTypesFromHand( ItemPackage.ItemPackageType.TwoHanded, hand.otherHand );
}

spawnedItem = GameObject.Instantiate( itemPackage.itemPrefab );
spawnedItem.SetActive( true );
hand.AttachObject( spawnedItem, attachmentFlags, attachmentPoint );

if ( ( itemPackage.otherHandItemPrefab != null ) && ( hand.otherHand.controller != null ) )
{
GameObject otherHandObjectToAttach = GameObject.Instantiate( itemPackage.otherHandItemPrefab );
otherHandObjectToAttach.SetActive( true );
hand.otherHand.AttachObject( otherHandObjectToAttach, attachmentFlags );
}

itemIsSpawned = true;

justPickedUpItem = true;

if ( takeBackItem )
{
useFadedPreview = true;
pickupEvent.Invoke();
CreatePreviewObject();
}
}
}
}

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2305
    • View Profile
    • jinxtergames
Re: Anyone know how to make an action for this script?
« Reply #1 on: July 24, 2017, 09:39:24 AM »
hi,
i think it is triggered from another script like for example :
Code: [Select]
private void OnHandHoverBegin( Hand hand )
Are you trying to get this script to work with your project or are you trying to remake this with Playmaker?

beowulfkaine

  • Playmaker Newbie
  • *
  • Posts: 44
    • View Profile
Re: Anyone know how to make an action for this script?
« Reply #2 on: July 24, 2017, 10:47:03 AM »
hi,
i think it is triggered from another script like for example :
Code: [Select]
private void OnHandHoverBegin( Hand hand )
Are you trying to get this script to work with your project or are you trying to remake this with Playmaker?

This script is from the SteamVR interactable demo scene.  It is the longbow part specifically. And in answer to your question , I'm definetly not trying to remake this script, I have no C# experience. I'm essentially trying to get it to work with playmaker.  My thought process is that there is some kind of method that is called by which I know how to do in playmaker with set property (i think)

here is the script I think you were talking about:

Code: [Select]
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: The object attached to the player's hand that spawns and fires the
// arrow
//
//=============================================================================

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Valve.VR.InteractionSystem
{
//-------------------------------------------------------------------------
public class ArrowHand : MonoBehaviour
{
private Hand hand;
private Longbow bow;

private GameObject currentArrow;
public GameObject arrowPrefab;

public Transform arrowNockTransform;

public float nockDistance = 0.1f;
public float lerpCompleteDistance = 0.08f;
public float rotationLerpThreshold = 0.15f;
public float positionLerpThreshold = 0.15f;

private bool allowArrowSpawn = true;
private bool nocked;

private bool inNockRange = false;
private bool arrowLerpComplete = false;

public SoundPlayOneshot arrowSpawnSound;

private AllowTeleportWhileAttachedToHand allowTeleport = null;

public int maxArrowCount = 10;
private List<GameObject> arrowList;


//-------------------------------------------------
void Awake()
{
allowTeleport = GetComponent<AllowTeleportWhileAttachedToHand>();
allowTeleport.teleportAllowed = true;
allowTeleport.overrideHoverLock = false;

arrowList = new List<GameObject>();
}


//-------------------------------------------------
private void OnAttachedToHand( Hand attachedHand )
{
hand = attachedHand;
FindBow();
}


//-------------------------------------------------
private GameObject InstantiateArrow()
{
GameObject arrow = Instantiate( arrowPrefab, arrowNockTransform.position, arrowNockTransform.rotation ) as GameObject;
arrow.name = "Bow Arrow";
arrow.transform.parent = arrowNockTransform;
Util.ResetTransform( arrow.transform );

arrowList.Add( arrow );

while ( arrowList.Count > maxArrowCount )
{
GameObject oldArrow = arrowList[0];
arrowList.RemoveAt( 0 );
if ( oldArrow )
{
Destroy( oldArrow );
}
}

return arrow;
}


//-------------------------------------------------
private void HandAttachedUpdate( Hand hand )
{
if ( bow == null )
{
FindBow();
}

if ( bow == null )
{
return;
}

if ( allowArrowSpawn && ( currentArrow == null ) ) // If we're allowed to have an active arrow in hand but don't yet, spawn one
{
currentArrow = InstantiateArrow();
arrowSpawnSound.Play();
}

float distanceToNockPosition = Vector3.Distance( transform.parent.position, bow.nockTransform.position );

// If there's an arrow spawned in the hand and it's not nocked yet
if ( !nocked )
{
// If we're close enough to nock position that we want to start arrow rotation lerp, do so
if ( distanceToNockPosition < rotationLerpThreshold )
{
float lerp = Util.RemapNumber( distanceToNockPosition, rotationLerpThreshold, lerpCompleteDistance, 0, 1 );

arrowNockTransform.rotation = Quaternion.Lerp( arrowNockTransform.parent.rotation, bow.nockRestTransform.rotation, lerp );
}
else // Not close enough for rotation lerp, reset rotation
{
arrowNockTransform.localRotation = Quaternion.identity;
}

// If we're close enough to the nock position that we want to start arrow position lerp, do so
if ( distanceToNockPosition < positionLerpThreshold )
{
float posLerp = Util.RemapNumber( distanceToNockPosition, positionLerpThreshold, lerpCompleteDistance, 0, 1 );

posLerp = Mathf.Clamp( posLerp, 0f, 1f );

arrowNockTransform.position = Vector3.Lerp( arrowNockTransform.parent.position, bow.nockRestTransform.position, posLerp );
}
else // Not close enough for position lerp, reset position
{
arrowNockTransform.position = arrowNockTransform.parent.position;
}


// Give a haptic tick when lerp is visually complete
if ( distanceToNockPosition < lerpCompleteDistance )
{
if ( !arrowLerpComplete )
{
arrowLerpComplete = true;
hand.controller.TriggerHapticPulse( 500 );
}
}
else
{
if ( arrowLerpComplete )
{
arrowLerpComplete = false;
}
}

// Allow nocking the arrow when controller is close enough
if ( distanceToNockPosition < nockDistance )
{
if ( !inNockRange )
{
inNockRange = true;
bow.ArrowInPosition();
}
}
else
{
if ( inNockRange )
{
inNockRange = false;
}
}

// If arrow is close enough to the nock position and we're pressing the trigger, and we're not nocked yet, Nock
if ( ( distanceToNockPosition < nockDistance ) && hand.controller.GetPress( SteamVR_Controller.ButtonMask.Trigger ) && !nocked )
{
if ( currentArrow == null )
{
currentArrow = InstantiateArrow();
}

nocked = true;
bow.StartNock( this );
hand.HoverLock( GetComponent<Interactable>() );
allowTeleport.teleportAllowed = false;
currentArrow.transform.parent = bow.nockTransform;
Util.ResetTransform( currentArrow.transform );
Util.ResetTransform( arrowNockTransform );
}
}


// If arrow is nocked, and we release the trigger
if ( nocked && ( !hand.controller.GetPress( SteamVR_Controller.ButtonMask.Trigger ) || hand.controller.GetPressUp( SteamVR_Controller.ButtonMask.Trigger ) ) )
{
if ( bow.pulled ) // If bow is pulled back far enough, fire arrow, otherwise reset arrow in arrowhand
{
FireArrow();
}
else
{
arrowNockTransform.rotation = currentArrow.transform.rotation;
currentArrow.transform.parent = arrowNockTransform;
Util.ResetTransform( currentArrow.transform );
nocked = false;
bow.ReleaseNock();
hand.HoverUnlock( GetComponent<Interactable>() );
allowTeleport.teleportAllowed = true;
}

bow.StartRotationLerp(); // Arrow is releasing from the bow, tell the bow to lerp back to controller rotation
}
}


//-------------------------------------------------
private void OnDetachedFromHand( Hand hand )
{
Destroy( gameObject );
}


//-------------------------------------------------
private void FireArrow()
{
currentArrow.transform.parent = null;

Arrow arrow = currentArrow.GetComponent<Arrow>();
arrow.shaftRB.isKinematic = false;
arrow.shaftRB.useGravity = true;
arrow.shaftRB.transform.GetComponent<BoxCollider>().enabled = true;

arrow.arrowHeadRB.isKinematic = false;
arrow.arrowHeadRB.useGravity = true;
arrow.arrowHeadRB.transform.GetComponent<BoxCollider>().enabled = true;

arrow.arrowHeadRB.AddForce( currentArrow.transform.forward * bow.GetArrowVelocity(), ForceMode.VelocityChange );
arrow.arrowHeadRB.AddTorque( currentArrow.transform.forward * 10 );

nocked = false;

currentArrow.GetComponent<Arrow>().ArrowReleased( bow.GetArrowVelocity() );
bow.ArrowReleased();

allowArrowSpawn = false;
Invoke( "EnableArrowSpawn", 0.5f );
StartCoroutine( ArrowReleaseHaptics() );

currentArrow = null;
allowTeleport.teleportAllowed = true;
}


//-------------------------------------------------
private void EnableArrowSpawn()
{
allowArrowSpawn = true;
}


//-------------------------------------------------
private IEnumerator ArrowReleaseHaptics()
{
yield return new WaitForSeconds( 0.05f );

hand.otherHand.controller.TriggerHapticPulse( 1500 );
yield return new WaitForSeconds( 0.05f );

hand.otherHand.controller.TriggerHapticPulse( 800 );
yield return new WaitForSeconds( 0.05f );

hand.otherHand.controller.TriggerHapticPulse( 500 );
yield return new WaitForSeconds( 0.05f );

hand.otherHand.controller.TriggerHapticPulse( 300 );
}


//-------------------------------------------------
private void OnHandFocusLost( Hand hand )
{
gameObject.SetActive( false );
}


//-------------------------------------------------
private void OnHandFocusAcquired( Hand hand )
{
gameObject.SetActive( true );
}


//-------------------------------------------------
private void FindBow()
{
bow = hand.otherHand.GetComponentInChildren<Longbow>();
}
}
}

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2305
    • View Profile
    • jinxtergames
Re: Anyone know how to make an action for this script?
« Reply #3 on: July 24, 2017, 11:03:40 AM »
Hi,
It is not that script :)
Maybe there is a script called "Interactable"
or look for scripts on the object (the one you talked about in the 1st post)

beowulfkaine

  • Playmaker Newbie
  • *
  • Posts: 44
    • View Profile
Re: Anyone know how to make an action for this script?
« Reply #4 on: July 24, 2017, 11:06:19 AM »
Hi,
It is not that script :)
Maybe there is a script called "Interactable"
or look for scripts on the object (the one you talked about in the 1st post)

here is one actually called "Interactable"

Code: [Select]
/======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: This object will get hover events and can be attached to the hands
//
//=============================================================================

using UnityEngine;
using UnityEngine.Events;
using System.Collections;

namespace Valve.VR.InteractionSystem
{
//-------------------------------------------------------------------------
public class Interactable : MonoBehaviour
{
public delegate void OnAttachedToHandDelegate( Hand hand );
public delegate void OnDetachedFromHandDelegate( Hand hand );

[HideInInspector]
public event OnAttachedToHandDelegate onAttachedToHand;
[HideInInspector]
public event OnDetachedFromHandDelegate onDetachedFromHand;

//-------------------------------------------------
private void OnAttachedToHand( Hand hand )
{
if ( onAttachedToHand != null )
{
onAttachedToHand.Invoke( hand );
}
}


//-------------------------------------------------
private void OnDetachedFromHand( Hand hand )
{
if ( onDetachedFromHand != null )
{
onDetachedFromHand.Invoke( hand );
}
}
}
}

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2305
    • View Profile
    • jinxtergames
Re: Anyone know how to make an action for this script?
« Reply #5 on: July 24, 2017, 11:25:05 AM »
Hi,
Hmm and this script is again talking to another script.
Maybe someone who has been using the VR can help you better.

or try to ask in the unity forums on how you could change it (even in code)
so they can give you some hints where to change things.
if you know where to, you can show here and i can continue to help if you want.