Log in or register to post comments

Play 2 animation with touch event

June 24, 2013 - 2:50am #1

Hello everyone,

I'm using Unity3D 4.0.1, i have one model object with 2 animation scene (scene A and B).

Firstly, my IT is playing animation (scene A) when marker was detected. I want to make my model to play other animation (scene B) by touch the model on my phone.

I have seen this link (https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/touch-object). I have added collider into my model, and then copy the code like in that link. I have a problem, my model doesn't switch animation when i touch the screen :(

This is my code :

using UnityEngine;
using System.Collections;

public class TouchDetection : MonoBehaviour {
	
	// Update is called once per frame
	void Update () {
		foreach (Touch touch in Input.touches)
		{
		    Ray ray = Camera.main.ScreenPointToRay(touch.position);
		    
		    if (touch.phase == TouchPhase.Began)
		    {
		        RaycastHit hit = new RaycastHit();
		        if (Physics.Raycast(ray, out hit, 1000))
		        {
		            Debug.Log("touched " + hit.transform.name);
		
					animation.Play("loncat");

		        }
		    }
		}
	}
}

Anyone can help me to fix that :D

Thanks,

Warm Regards.

- denmas

 

Play 2 animation with touch event

December 9, 2013 - 7:31pm #10

   I have done with this code below. Try this :

 

if (Physics.Raycast(ray, out hit)) {

    hit.transform.gameObject.animation["jump_pose"].wrapMode = WrapMode.Loop; //for animation type wrap mode
    hit.transform.gameObject.animation.Play("jump_pose");

}

 

 

Play 2 animation with touch event

July 4, 2013 - 6:36pm #9

Hello, please help me to solve my problem here  :''(

Thanks,

- denmas

Play 2 animation with touch event

July 1, 2013 - 7:17pm #8

Hi DavidBeard, sorry if i bother :D

Yes, i have attached OnTouchDown to ARCamera and ObjectHighlighter to my object, and then create collider to my object too. But when i run the app, i don't know that scripts work or not. 

Can you give to me screenshoot from the result of that script (whether the object has higlighted or not) ?

Then, i've got an error here. Here is the error : 

NullReferenceException
UnityEngine.GameObject.GetComponent[MeshRenderer] () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:27)
ObjectHighlighter.Start () (at Assets/ObjectHighlighter.cs:19)

The line 19 is : 

meshRenderer 		= baseObject.GetComponent<MeshRenderer>();

Ehm, do you have idea to solve this problem ?

Thanks,

- denmas

AttachmentSize
Image icon Capture.PNG25.57 KB

Play 2 animation with touch event

July 1, 2013 - 12:55pm #7

Yes add OnTouchDown to the ARCamera. Also you need to retain the SendMessage statement to call the highlighter. Otherwise the object is never notified of the touch event.

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

public class OnTouchDown : MonoBehaviour
{
    void Update () {
        RaycastHit hit = new RaycastHit();
        for (int i = 0; i < Input.touchCount; ++i) {
            if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {
            // Construct a ray from the current touch coordinates
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
            if (Physics.Raycast(ray, out hit)) {
                hit.transform.gameObject.SendMessage("OnMouseDown");
              }
           }
       }
    }
}

Play 2 animation with touch event

July 1, 2013 - 12:54pm #6

Hmm, i have tried to attach your code to my empty gameObject (like in your post here:https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/detecting-touch-object).

void Update () {
 
if (Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
ShootRay(ray);
}
}
 
void ShootRay (Ray ray)
{  
 
RaycastHit rhit;
 
bool objectHit = false;
GameObject gObjectHit = null;
 
 
if (Physics.Raycast (ray, out rhit, 1000.0f)) {
  Debug.Log("Ray Shot and hit!");
  objectHit = true;
  gObjectHit = rhit.collider.gameObject;
}
 
}

My question is, Whether i should attach OnTouchDown.cs to ARCamera prefab or not?

And then how can i make sure this code ( Debug.Log("Ray Shot and hit!"); ) works on log display in Unity when i tap the screen? (I'm newbie, so i haven't familiar with Unity3D) :D

I ask like that because the AR part works but i don't know whether log works when i tap the screen. :(

Thanks,

- denmas

Play 2 animation with touch event

July 1, 2013 - 4:22am #5

Sorry, I am not familiar with the ObjectHighlighter script.

Does the OnTouchDown script work at all?

If not, I suggest you try with a simple non AR project containing a simple cube, and then build up from there.

 

N

Play 2 animation with touch event

July 1, 2013 - 3:24am #4

Hai NalinS,

I have tried like in this post : https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/detecting-touch-object . I have attached OnTouchDown.cs to ARCamera prefab and ObjectHighlighter.cs to my object (nyancat.obj) and then create collider for it.  :D

The AR part works fine but why my object doesn't highlighted :(  where's my fault ???

Please look my image below, is there something wrong with my collider ?

Thanks before,

- denmas

AttachmentSize
Image icon Untitled.png563.71 KB

Play 2 animation with touch event

June 24, 2013 - 7:00pm #3

Sorry Nalin S,

Actually, i'm still newbie in Unity3D and don't know what's step to fix my problem XD

So, thanks for your help and i'll try to follow your guide.

Thanks :D

Play 2 animation with touch event

June 24, 2013 - 5:41am #2

I think you should search the forums for "Raycast" as this has come up many times before, though strictly speaking this is not really a Vuforia issue, but more about Unity.

I have a problem, my model doesn't switch animation when i touch the screen

In future please try and provide more information than this, because basically you have said "it does not work" without trying to break the problem down and describe what else you have tried - being more specific is helpful to people to provide you with answers.

 

It would be an idea to see if you can get a touch to detect on an object in the first place, like I described in this post:

https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/detecting-touch-object

or this one:  https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/how-add-touch-listener-3d-object-unity

 

If this works then you can transport it into an AR scene.

Another thread that might help you is this one:

https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/raycast-ar-camera

 

Finally if you are having problems playing animations, then this thread may help:

https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/how-check-if-child-image-target-showed#comment-2028342

 

HTH

N

 

 

 

Log in or register to post comments