Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Leap: Touch object multiple times, different behavior.

Discussion in 'AR/VR (XR) Discussion' started by fredyyanez, Jun 5, 2015.

  1. fredyyanez

    fredyyanez

    Joined:
    Jul 8, 2014
    Posts:
    33
    Essentially, I want to be able to touch object multiple times and have the object behave differently each time.
    When I touch it the first time it would turn red ,the second time it would turn blue and the third time it will turn back to white.
    Touch 0: White
    Touch 1: Turns Red
    Touch 2: Turns Blue
    Touch 3: Turn back to White
    This is my code, but the issue is that the OnTriggerEnter happens every frame so the shape would turn from white to blue with out turning red..etc

    Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShapeScript : MonoBehaviour {
    5.  
    6.     public bool touched;
    7.     public bool touched2;
    8.     public bool enter;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         touched = false;
    13.         touched2 = false;
    14.         enter = false;
    15.         //InvokeRepeating ("UpdateColor", 0, .7f);
    16.    
    17.     }
    18.     private bool IsHand(Collider other)
    19.     {
    20.         if (other.transform.parent && other.transform.parent.parent && other.transform.parent.parent.GetComponent<HandModel>())
    21.             return true;
    22.         else
    23.             return false;
    24.     }
    25.  
    26.     IEnumerator OnTriggerEnter( Collider col){
    27.  
    28.         if (IsHand(col)) {
    29.             enter=true;
    30.             //Debug.Log( touched +" " +touched2);
    31.             if(!touched && !touched2 && enter){
    32.                 touched=true;
    33.                 touched2=false;
    34.            
    35.  
    36.             }
    37.             else if(touched && !touched2 && enter){
    38.                 touched=false;
    39.                 touched2=true;
    40.  
    41.             }
    42.             else if(!touched && touched2 && enter){
    43.                 touched=false;
    44.                 touched2=false;
    45.             }
    46.             yield return new WaitForSeconds(1);
    47.  
    48.            
    49.         }
    50.  
    51.  
    52.  
    53.     }
    54.     void OnTriggerExit(){
    55.         enter = false;
    56.  
    57.     }
    58.    
    59.     // Update is called once per frame
    60.     void UpdateColor(){
    61.  
    62.         if (touched) {
    63.             gameObject.GetComponent<Renderer> ().material.color = Color.red;
    64.  
    65.  
    66.         } else if (touched2) {
    67.             gameObject.GetComponent<Renderer> ().material.color = Color.blue;
    68.         }
    69.         else {
    70.             gameObject.GetComponent<Renderer> ().material.color = Color.white;
    71.         }
    72.    
    73.     }
    74.  
    75.     void Update(){
    76.        UpdateColor ();
    77.         if (touched && HandScript.enable  ) {
    78.             gameObject.transform.Rotate(Vector3.up, HandScript.handRotation.y);
    79.  
    80.  
    81.         }
    82.         if (touched2 && HandScript.enable) {
    83.             gameObject.transform.Rotate(Vector3.left, HandScript.handRotation.x);
    84.         }
    85.  
    86.     }
    87. }
    88.  
    There is a lot of overlap because I've been trying to find a fix.

    Any help would be appreciated!!
     
  2. fredyyanez

    fredyyanez

    Joined:
    Jul 8, 2014
    Posts:
    33