I'm new to UNITY. Its a simple question but I'm not able to do this... I have model in my scene with animation and animator. The default state of model is IDLE. I want it to go to Moving State when I touch that MODEL on my Mobile screen. I'm able to animate this when i touch anywhere on screen ... But I want it to play "moving" animation when I touch that particular 3D Object/Model in scene. Here is my Code :
void Start () {
ani = GetComponent<Animator>();
}
void Update() {
//FOR MOUSE
//if (Input.GetMouseButtonDown (0)) {
// Debug.Log ("mouse button was pressed");
// ani.Play ("moving",-1,0f);
//FOR MOBILE
if (Input.touchCount == 1) {
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began) {
ani.Play ("moving", -1, 0f);
}
I attached this script on Model. I tried out diffrent method but nothing is working:
//NOT WORKING
//if(Input.GetMouseButton(0))
// {
// Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// RaycastHit hit;
//
// if(Physics.Raycast(ray, out hit, 100))
// {
// ani = hit.collider.GetComponent<Animator>();
// ani.Play("moving",-1,0f);
// }
AND
// NOT WORKING
//if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
// {
// RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
// if(hit.collider != null)
// {
// Debug.Log ("Touched it");
// ani.Play ("moving", -1, 0f);
// }
// }
I know this is small code ...please tell me where I'm wrong ?