Search Unity

Problem with raycast

Discussion in 'Animation' started by herbie, May 6, 2015.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Hi,

    For my Android game I have animated a character with mecanim. When I touch this character, it starts walking (see script).
    Code (JavaScript):
    1. function Update ()
    2. {
    3.        for (var touch: Touch in Input.touches)
    4.        {
    5.            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    6.         {
    7.             var ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    8.             var hit: RaycastHit;
    9.    
    10.             if(Physics.Raycast(ray, hit))
    11.             {
    12.                 // start walking
    13.             }
    14.         }
    15.     }
    16. }
    17.  
    Now I have a second character. It's a duplicate of the first one but all the names, animator controller, etc are changed. But when I hit one of the characters, both characters are walking. What I would like to have is that only the character that you hit is gonna walk.
    I think I have overlooked something. Why is there a connection between the two different raycasts?
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Since they both check against a single touch - any single touch anywhere - they will both do whatever you tell them to do. If the walking is just happening whenever that ray hits something they will walk even if the ray hits the ground. Your gonna need to make it so you hit each one separately somehow, like a button assigned to walk each different character, or check if they are hit by a Raycast (by checking the hit info when that Raycast hits something, perhaps check if it's this.gameObject and then walk)