- Home /
 
               Question by 
               Skunklomenos · Feb 16, 2014 at 08:19 PM · 
                c#animationraycasttouch  
              
 
              How to select an object with TOUCH and change its animation 2D
Here is the thing! I want to Touch a particular object in the screen and then change its animation. I came up to this so far: using UnityEngine; using System.Collections;
 public class Cop : MonoBehaviour {
     Animator anim;
     void Start () 
     {
         anim = GetComponent<Animator> ();
     }
     void FixedUpdate () 
     {
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began ) 
         {
             if(anim.GetBool("IsSelected")==false){
                 anim.SetBool ("IsSelected",true);
             }
             else{
                 anim.SetBool ("IsSelected",false);
             }
         }
     }
 }
This changes the animation but when i press somewhere on the screen,but i want to change it only when the touch is on the particular object.I think something has to be done with raycast but i dont know what :( . Thanks in Advance!
               Comment
              
 
               
              Why don't you convert it into answer. So that others can see it and may be upvote.
 
               Best Answer 
              
 
              Answer by Skunklomenos · Feb 16, 2014 at 08:28 PM
I've hopefully found the answer at last..
 using UnityEngine;
 using System.Collections;
 
 public class Target: MonoBehaviour {
 
     public    Animator anim;
     bool isSelected;
     void Start () 
     {
         anim = GetComponent<Animator> ();
     }
     void Update(){
         
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
             
             Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
             RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction);
             //Select Hool
             if (hit != null && hit.collider.transform.name == "Target") {
                 isHit = false;
                 //Destroy(GameObject.Find(hit.collider.gameObject.name));
                 if (anim.GetBool ("IsSelected") == false) {
                     anim.SetBool ("IsSelected", true);
                     isSelected=true;
 
                 } else {
                     anim.SetBool ("IsSelected", false);
                     isSelected=false;
                 }
             }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                