Log in or register to post comments

Script Help

November 7, 2014 - 11:58am #1

I have this script attach to a Capsule. How can i attach a Vuforia script, 

how can I adapt this script for, On target found, the capsule execute the script.

 

 var speed : float = 1.0;
 var rotateSpeed : float = 20.0; // rotate speed in degrees per second
 var gravity : float = 10.0;
 private var moveDirection : Vector3 = Vector3.zero;
 private var curDir = 1;
 function Update () {
 var controller: CharacterController = GetComponent(CharacterController);
 if ( controller.isGrounded ) {
 if (Random.value < 0.05f){ // if random value < 5/100...
curDir = -curDir; // flip direction
 }
 // Rotate around y - axis
transform.Rotate(Vector3.up * curDir * rotateSpeed * Time.deltaTime);
moveDirection = speed * transform.TransformDirection(Vector3.forward);
 }
moveDirection.y -= gravity * Time.deltaTime; // apply gravity
controller.Move(moveDirection * Time.deltaTime);
 } 

Script Help

November 11, 2014 - 11:23am #11

I put Animtation.Stop in OnTrackingLost but stil playing the animation. here is the code.

 

/*==============================================================================
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Qualcomm Connected Experiences, Inc.
==============================================================================*/

using UnityEngine;

/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour,
                                            ITrackableEventHandler
{
    #region PRIVATE_MEMBER_VARIABLES
 
    private TrackableBehaviour mTrackableBehaviour;
    
    #endregion // PRIVATE_MEMBER_VARIABLES



    #region UNTIY_MONOBEHAVIOUR_METHODS
    
    void Start()
    {
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
    }

    #endregion // UNTIY_MONOBEHAVIOUR_METHODS



    #region PUBLIC_METHODS

    /// <summary>
    /// Implementation of the ITrackableEventHandler function called when the
    /// tracking state changes.
    /// </summary>
    public void OnTrackableStateChanged(
                                    TrackableBehaviour.Status previousStatus,
                                    TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            OnTrackingFound();
        }
        else
        {
            OnTrackingLost();
        }
    }

    #endregion // PUBLIC_METHODS



    #region PRIVATE_METHODS


    private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);


//	 *** Additional animation code
//		Animation[] animationComponents = GetComponentsInChildren<Animation>();
//		
//		foreach (Animation component in animationComponents)
//		{
//			component.Play();  
//		}
//	 *** end of animation code

		Animation[] animationComponents = GetComponentsInChildren<Animation>();
		
		foreach (Animation animation in animationComponents)
		{
			foreach (AnimationState animState in animation)
			{
				animState.speed = 1f;
				animation.Play();
			}
		}




        // Enable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = true;
        }

        // Enable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = true;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    }


    private void OnTrackingLost()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);


		Animation[] animationComponents = GetComponentsInChildren<Animation>();
		
		foreach (Animation animation in animationComponents)
		{
			foreach (AnimationState animState in animation)
			{
				animState.speed = 0f;
				animation.Stop();
			}
		}



        // Disable rendering:
        foreach (Renderer component in rendererComponents)
        {
            component.enabled = false;
        }

        // Disable colliders:
        foreach (Collider component in colliderComponents)
        {
            component.enabled = false;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    }

    #endregion // PRIVATE_METHODS
}

 

Script Help

November 11, 2014 - 11:13am #10

You can use Animtation.Stop in OnTrackingLost and Animation.Play in OnTrackingFound. The rest is just application logic.

See also:

http://docs.unity3d.com/ScriptReference/Animation.html

 

Script Help

November 11, 2014 - 7:11am #9

Hi Alessandro. I put this extra code to the DefaultTrackableEventHandler, now the animation works fine at start.

How can i do, once the animation is completed, leave as finished without that every time the camera comes out of the target image and back, having to restart it.

 

// *** Additional animation code
        Animation[] animationComponents = GetComponentsInChildren<Animation>();
        
        foreach (Animation component in animationComponents)
        {
            component.Play();  
        }
        // *** end of animation code

 

Script Help

November 10, 2014 - 7:48am #8

Ok, I managed to disable the animation, but now i need to make the animation start, at image target found. Please give me a help on the script.

Script Help

November 10, 2014 - 7:11am #7

Animations in Unity start automatically if the "Play Automatically" flag is enabled in the unity Inspector; you will need to disable that flag, see:

http://gamedev.stackexchange.com/questions/79495/how-to-disable-play-automatically-on-animation-in-unity3d-4-5-1

 

Script Help

November 10, 2014 - 6:49am #6

i create a java script and paste this.

 

#pragma strict

function Start () {

}
    // Plays the default animation
    animation.Play();
    
    
function Update () {

}

http://www.nabilfx.com/IMG_4115.MOV

But still the same, the animaton start before, the image target found.

Script Help

November 10, 2014 - 6:12am #5

For animations, you probably want to use the Unity API here;

http://docs.unity3d.com/ScriptReference/Animation.Play.html

 

Script Help

November 10, 2014 - 6:03am #4

I did another example, a Cube with a 50 frames animation, i made the animation in Unity, i need to start the Cube animation on tracker found, can u show me how to do that, cause i turn of, play animarion at start, nevertheless the animation start before target found, I need it to start on image target found.

Script Help

November 10, 2014 - 5:49am #3

Can i direct the ITrackableEventHandler, to this java script?

Script Help

November 10, 2014 - 5:35am #2

You can detect an 'On target found' condition by using the DefaultTrackableEventHandler script that comes with any Trackable prefab (see also the Vuforia Samples), or by implementing your own script that implements the ITrackableEventHandler interface and registering it with the trackableBehaviour in question.

From there, you can then trigger any custom script code.

Note that the script you posted is written in Javascript, whereas Vuforia API is based on C#. 

This will pose additional challenges at general Unity programming level, which you may want to address in the Unity forums (not Vuforia).

 

Log in or register to post comments