Hi guys,
This is the first time I post in here. I’m an absolute newbie on Unity, and I’m trying to make an application to run on my Android phone with augmented reality (Vuforia). I already have the augmented part working fine, but I’m wrecking my brains with the two next steps. All I need is to control the animation of an imported FBX through touch on the phone screen (doesn’t need to be on an specific area, it could be the entire screen). Oh, and I already have Android Studio installed, so the deployment of the .apk fro Unity is working fine too.
I already created many animation takes on the imported FBX, according to the frame I need, but since I’m pretty new at Unity, I don’t know how to write the code to do this:
[Touch on the screen] ----> play take 01 ----> [stop animation] ----> [Touch] ------> play take 02 -----> [stop animation] and so on…
I think it has something to do with Animator, but can’t figure out how exactly. What I tried:
var cubo : GameObject;
cubo = GameObject.Find(“cubo”);
if (Input.touchCount = 1) {
cubo.animation.Play( “take_02” );
}
if (Input.touchCount = 2) {
cubo.animation.Play( “take_03” );
}
}
But obviously doesn’t work. =(
Can anyone at least point me in the right direction, please?
Thanks.
A little update… After wrecking my brains for a while, a managed to do this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cuboAnimController : MonoBehaviour {
public Animator cuboAnim;
// Use this for initialization
void Start () {
cuboAnim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
cuboAnim.Play("take_01");
if (Input.GetMouseButtonDown(0))
cuboAnim.Play("take_02");
if (Input.GetMouseButtonDown(0))
cuboAnim.Play("take_03");
if (Input.GetMouseButtonDown(0))
cuboAnim.Play("take_04");
}
}
Its simple, but remember, I have ZERO background in programming, so even this was pretty difficult to achieve… but It doesn´t work the way I need yet. For some reason, it only plays two clips back and forth, and from bottom to top (in this case, first tap plays “take_04”, then “take_03”, than “take_04” again, than “take_03” again…) Is there a way to make the other screen touches play the other animation clips? And in the top-bottom order, if possible?
All I need is to play a sequence of animation clips by tapping on the screen…
Any help?
Well, after battle with this for God knows how many hours and with lumps of hair missing, I finally got it to work properly, and thanks to people from another forum.
Anyway, for those who are having the same problem, here is the solution:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cuboAnimController : MonoBehaviour {
int numAnim = 0;
Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
anim.SetInteger("condicao", numAnim);
if (Input.GetMouseButtonDown(0))
{
if (numAnim <= 10)
numAnim += 1;
else numAnim = 0;
}
}
}
You have to create an int parameter called “condicao” (or any other name) inside the animator controller and make the transitions between the clips.
Thanks.