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. Dismiss Notice

Animate when Joystick is pressed

Discussion in 'Animation' started by Tobl1x, Feb 13, 2021.

  1. Tobl1x

    Tobl1x

    Joined:
    Feb 12, 2021
    Posts:
    5
    Hi
    Im just started with Untiy and want to make a 3d Android Game. I Watched a few tutorials and managed to get the joystick to work. I downloaded a free character asset with animation and also made the animation work when a bool ("Movement" in the code) is true. This Variable should be true when the joystick is touched or even better when the charakter is moving. I tried 2 Versions of Code but none of them worked.
    Can sombody help me how i can get this to work.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TouchDetetion : MonoBehaviour
    6. {
    7.     public Animator anim;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if (Input.touchCount > 0)
    17.         {
    18.             if (Input.GetTouch(0).phase == TouchPhase.Began)
    19.             {
    20.                 anim.SetBool("Movement",true);
    21.             }
    22.             if (Input.GetTouch(0).phase == TouchPhase.Ended)
    23.             {
    24.                 anim.SetBool("Movement",false);
    25.             }
    26.         }
    27.     }
    28. }
    and

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TouchDetetion : MonoBehaviour
    6. {
    7.     public Animator anim;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         anim = GetComponent<Animator>();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if (Input.touchCount > 0)
    17.         {
    18.             anim.SetBool("Movement", true);
    19.         }
    20.         else
    21.         {
    22.             anim.SetBool("Movement",false);          
    23.         }
    24.     }
    25. }
     
  2. Samwholearns

    Samwholearns

    Joined:
    Feb 14, 2021
    Posts:
    1
    I'm going to help you help yourself.

    First things first, as a programmer, we have tools that can be used to help us figure out why things aren't working. Debug.Log is great for this. Try using Debug.Log to see the state of the boolean you created. See if it is true or false in real time.

    This can lead the next question "Why is it true when I think it should be false?" or "Why is it false when I think it should be true?"

    With print statements you can ask more specific questions than "why isn't this working?" that will lead you to a working implementation.
     
    Tobl1x likes this.
  3. Tobl1x

    Tobl1x

    Joined:
    Feb 12, 2021
    Posts:
    5
    I will try that, thanks!
     
  4. Tobl1x

    Tobl1x

    Joined:
    Feb 12, 2021
    Posts:
    5
    Ok i tried it and found out that the touches arent recognized. Do you know how i can make a working touch detection?