Search Unity

Missing touches for 2D GUITexture

Discussion in 'Scripting' started by Nerevarine, Jul 26, 2014.

  1. Nerevarine

    Nerevarine

    Joined:
    Jul 26, 2014
    Posts:
    14
    Im making a 2D game which currently has 3 touch buttons, Move Left, Move Right and Jump.. Im using the standard charecter 2D controller from the unity standard assets, and a custom script for touch input.. My problem is, the jump button is sometimes not registering.. The move left and move right buttons are working as intended...
    Testing this on the PC, everything seems to be working fine (Jump is mapped to Spacebar and movement to arrow keys)..
    What could be the problem ?
    Touchinput code :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TouchInput1 : MonoBehaviour {
    5.  
    6.     private PlatformerCharacter2D character;
    7.  
    8.     public Texture2D textureButtonLeft;
    9.     public Texture2D textureButtonRight;
    10.     public Texture2D textureButtonJump;
    11.  
    12.     bool Right = false;
    13.     bool Left = false;
    14.  
    15.  
    16.     bool jump = false;
    17.     float m=0;
    18.  
    19.     float w =0;
    20.     float h=0;
    21.  
    22.  
    23.  
    24.     // Use this for initialization
    25.     void Start ()
    26.     {
    27.         w=Screen.width;
    28.         h=Screen.height;
    29.  
    30.     }
    31.  
    32.  
    33.  
    34.     void OnGUI()
    35.     {
    36.      
    37.         GUI.DrawTexture(new Rect(w-w/8, h-h/5,100, 100 ),textureButtonJump);
    38.         GUI.DrawTexture(new Rect( w/10, h-h/5,100, 100),textureButtonLeft);
    39.         GUI.DrawTexture(new Rect( w/10+110, h-h/5,100,100 ),textureButtonRight);
    40.  
    41.  
    42.     }
    43.  
    44.  
    45.     void Awake()
    46.     {
    47.         character = GetComponent<PlatformerCharacter2D>();
    48.      
    49.     }
    50.  
    51.  
    52.     // Update is called once per frame
    53.     void FixedUpdate ()
    54.     {
    55.         Rect rightIconRect = new Rect( w/10+110, h-h/5,100,100);
    56.         Rect leftIconRect = new Rect ( w/10, h-h/5,100, 100);
    57.         Rect jumpIconRect = new Rect (w/2, 0,w/2, h);
    58.  
    59.         foreach(Touch t in Input.touches)
    60.         {
    61.             Vector2 vec = t.position;
    62.             vec.y = Screen.height - vec.y; // You need to invert since GUI and screen have differnet coordinate system
    63.             if(rightIconRect.Contains(vec)) Right = true;
    64.             if(leftIconRect.Contains(vec)) Left = true;
    65.  
    66.             if (jumpIconRect.Contains(vec) && t.phase == TouchPhase.Began )
    67.             {
    68.                 Debug.Log ("jump button pressed");
    69.                 jump = true;
    70.  
    71.             }
    72.  
    73.  
    74.         }
    75.  
    76.         if (Right == true)
    77.         {
    78.             m=1;
    79.         }
    80.      
    81.         if (Left == true)
    82.         {
    83.             m=-1;
    84.         }
    85.  
    86.         character.Move( m, false , jump);
    87.         Right = false; Left = false;
    88.         m=0; jump= false;
    89.  
    90.     }
    91. }
    92.  
    Controller Code (standard asset)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class PlatformerCharacter2D : MonoBehaviour
    5. {
    6.     bool facingRight = true;                            // For determining which way the player is currently facing.
    7.  
    8.     [SerializeField] float maxSpeed = 10f;                // The fastest the player can travel in the x axis.
    9.     [SerializeField] float jumpForce = 400f;            // Amount of force added when the player jumps.
    10.  
    11.     [Range(0, 1)]
    12.     [SerializeField] float crouchSpeed = .36f;            // Amount of maxSpeed applied to crouching movement. 1 = 100%
    13.  
    14.     [SerializeField] bool airControl = false;            // Whether or not a player can steer while jumping;
    15.     [SerializeField] LayerMask whatIsGround;            // A mask determining what is ground to the character
    16.  
    17.     Transform groundCheck;                                // A position marking where to check if the player is grounded.
    18.     float groundedRadius = .2f;                            // Radius of the overlap circle to determine if grounded
    19.     public bool grounded = false;                                // Whether or not the player is grounded.
    20.     Transform ceilingCheck;                                // A position marking where to check for ceilings
    21.     float ceilingRadius = .01f;                            // Radius of the overlap circle to determine if the player can stand up
    22.     Animator anim;                                        // Reference to the player's animator component.
    23.  
    24.  
    25.     void Awake()
    26.     {
    27.         // Setting up references.
    28.         groundCheck = transform.Find("GroundCheck");
    29.         ceilingCheck = transform.Find("CeilingCheck");
    30.         anim = GetComponent<Animator>();
    31.     }
    32.  
    33.  
    34.     void FixedUpdate()
    35.     {
    36.         // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    37.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);
    38.         anim.SetBool("Ground", grounded);
    39.  
    40.         // Set the vertical animation
    41.         anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
    42.     }
    43.  
    44.  
    45.     public void Move(float move, bool crouch, bool jump)
    46.     {
    47.  
    48.  
    49.         // If crouching, check to see if the character can stand up
    50.         if(!crouch && anim.GetBool("Crouch"))
    51.         {
    52.             // If the character has a ceiling preventing them from standing up, keep them crouching
    53.             if( Physics2D.OverlapCircle(ceilingCheck.position, ceilingRadius, whatIsGround))
    54.                 crouch = true;
    55.         }
    56.  
    57.         // Set whether or not the character is crouching in the animator
    58.         anim.SetBool("Crouch", crouch);
    59.  
    60.         //only control the player if grounded or airControl is turned on
    61.         if(grounded || airControl)
    62.         {
    63.             // Reduce the speed if crouching by the crouchSpeed multiplier
    64.             move = (crouch ? move * crouchSpeed : move);
    65.  
    66.             // The Speed animator parameter is set to the absolute value of the horizontal input.
    67.             anim.SetFloat("Speed", Mathf.Abs(move));
    68.  
    69.             // Move the character
    70.             rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
    71.          
    72.             // If the input is moving the player right and the player is facing left...
    73.             if(move > 0 && !facingRight)
    74.                 // ... flip the player.
    75.                 Flip();
    76.             // Otherwise if the input is moving the player left and the player is facing right...
    77.             else if(move < 0 && facingRight)
    78.                 // ... flip the player.
    79.                 Flip();
    80.         }
    81.  
    82.         // If the player should jump...
    83.         if (grounded && jump) {
    84.             // Add a vertical force to the player.
    85.             anim.SetBool("Ground", false);
    86.             rigidbody2D.AddForce(new Vector2(0f, jumpForce));
    87.         }
    88.     }
    89.  
    90.  
    91.     void Flip ()
    92.     {
    93.         // Switch the way the player is labelled as facing.
    94.         facingRight = !facingRight;
    95.      
    96.         // Multiply the player's x local scale by -1.
    97.         Vector3 theScale = transform.localScale;
    98.         theScale.x *= -1;
    99.         transform.localScale = theScale;
    100.     }
    101. }
    102.  
    103.  
     
  2. Nerevarine

    Nerevarine

    Joined:
    Jul 26, 2014
    Posts:
    14
    Okay I noticed that it only happens when I press jump WHILE im already in the air (i.e. grounded == false)
    The next jump button even when im in ground, gets missed or bugged..
    Any advice on how to solve this, I tried
    1. if (jumpIconRect.Contains(vec) && t.phase == TouchPhase.Began && character.grounded == true )
    2. {
    3. code
    4. }
    but still doesnt work
     
  3. Nerevarine

    Nerevarine

    Joined:
    Jul 26, 2014
    Posts:
    14
    bump, please guys..
     
  4. Nerevarine

    Nerevarine

    Joined:
    Jul 26, 2014
    Posts:
    14
    Fixed, I used the Update function for Jump button instead of FixedUpdate, really nooby of me