0

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 ?

Nitesh
  • 131
  • 1
  • 16
  • Go to **5.For 3D Object (Mesh Renderer/any 3D Collider)** on the duplicated answer. Don't forget to attach collider to your model. – Programmer Feb 15 '17 at 19:23
  • box collider works but mesh collider not – Nitesh Mar 12 '17 at 19:55
  • This is not true. A collider is always a collider. It doesn't matter if Mesh or Box Collider. This works on any collider. If you have a problem, create new question but provide your code that doesn't work. Also, provide a screenshot that shows the collider settings of the object you want to detect click on. You are definitely doing something wrong! – Programmer Mar 12 '17 at 21:07
  • `foreach (Touch touch in Input.touches) { Ray ray = Camera.main.ScreenPointToRay(touch.position); //Debug.Log("RAY:" + ray.direction); if (touch.phase == TouchPhase.Began) { Debug.Log("touched"); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(ray, out hit, 1000)) { Debug.DrawRay(ray.origin, ray.direction, Color.green); Debug.Log("Ray touched " + hit.transform.name); nam = hit.transform.name; ani.Play ("moving", -1, 0f); }` working fine with box/sphere collider bot not with mesh – Nitesh Mar 13 '17 at 21:06
  • The code you have has nothing to do with the code in the answer from the duplicated question. I did not perform any raycast. This will be my last reply to this post. You can ask a new question with the correct code. If you use that code in your above comment for your new question, I will likely close that new question too. Read that post! Read from **5.For 3D Object (Mesh Renderer/any 3D Collider)** – Programmer Mar 13 '17 at 21:12

0 Answers0