Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make character change form when tapped and change back when released

Discussion in 'Scripting' started by xfactor00, Oct 22, 2014.

  1. xfactor00

    xfactor00

    Joined:
    Oct 22, 2014
    Posts:
    2
    Hi there,

    I'm making a 2d mobile game in unity and what I want is for my character to change form into a different version of himself when the screen is tapped and to change back on release.

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Basically what you want is to have two sprites and switch between them based on the value (true/false) returned by Input.GetMouseButton(0)

    I threw together a little "TouchToSmile" folder; launch the scene and take a look at its setup, a single sprite and a script containing the code to check the screen, copied here for your reference:

    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. publicclassTouchToSmile : MonoBehaviour
    6. {
    7. publicSpritespriteNormal;
    8. publicSpritespriteTouched;
    9.  
    10. SpriteRenderersr;
    11.  
    12. voidStart ()
    13.  {
    14. sr = gameObject.GetComponent<SpriteRenderer>();
    15.  }
    16.  
    17. voidUpdate ()
    18.  {
    19. if (Input.GetMouseButton(0))
    20.  {
    21. sr.sprite = spriteTouched;
    22.  }
    23. else
    24.  {
    25. sr.sprite = spriteNormal;
    26.  }
    27.  }
    28. }
    Kurt
     

    Attached Files:

  3. xfactor00

    xfactor00

    Joined:
    Oct 22, 2014
    Posts:
    2
    Thanks a lot!