Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SnowCoding · Dec 30, 2013 at 03:02 AM · c#2djumpingplatformerrunning

2D Platformer Jump while Running Android

Hi Im new to the unity community but not Unity and programming. I am making a 2D Platformer in Unity 4.3 (new 2D Tools) and everything seemed to be going just fine until I added Jumping... My character Jumps fine but my problem is that he wont jump while Running left or right. My character does this fine on a windows computer but when I compile a .APK and run it on my android tablet with touch controls (GuiTextures) he simply will not Jump while running left or Right.

Here are my scripts...

PlayerLogic.cs using UnityEngine; using System.Collections;

 public class PlayerLogic : MonoBehaviour {
 
     public Transform GroundStart, GroundEnd;
     public bool grounded  = false;
     public float jumpForce = 300f;
     public float MoveSpeed = 3f;
     
     public int JumpCount = 0;
 
     //Movement booleans
     public static bool MoveRight = false;
     public static bool MoveLeft = false;
     public static bool Jump = false;
     // Use this for initialization
     void Update()
     {
         //Run these to functions
         Raycasting();
         Movement();
     }
 
     void Raycasting()
     {
         //Draw a line from the start position, to the end position.
         Debug.DrawLine(this.transform.position, GroundEnd.position, Color.green);
         //If Raycast detects the Ground layer from the end point of the line, then IsGrounded becomes true
         grounded = Physics2D.Linecast(this.transform.position, GroundEnd.position, 1 << LayerMask.NameToLayer("Ground"));
     }
 
     void Movement()
     {
         //Touch Controls
 
         if(MoveRight == true)
         {
             transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         if(MoveLeft == true)
         {
             transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 180);  
         }
         
         if(Jump == true && grounded == true)
         {
             rigidbody2D.AddForce(Vector2.up * jumpForce);
             Jump = false;
         }
 
         //Key Controls
 
         /*if(Input.GetKey (KeyCode.D))
         {
             transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         if(Input.GetKey (KeyCode.A))
         {
             transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 180);  
         }
         
         if(Input.GetKeyDown (KeyCode.Space) && grounded)
         {
             rigidbody2D.AddForce(Vector2.up * jumpForce);
         }*/
         
     }
 }

PlayerLeft.cs

 using UnityEngine;
 using System.Collections;
 
 public class PlayerLeft : MonoBehaviour {
     
     // Update is called once per frame
     void Update () 
     {
         //Is there a touch?
         if(Input.touches.Length <= 0)
         {
             //If no touches...
         }
         else //If screen touched..
         {
             //Loop through these
             for(int i = 0; i < Input.touchCount; i++)
             { 
                 //executes this code for current touch
                 if(this.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
                     //If current touch hits the guitexture...
                     if(Input.GetTouch(i).phase == TouchPhase.Began)
                     { 
                         PlayerLogic.MoveLeft = true;
                     }
                     if(Input.GetTouch(i).phase == TouchPhase.Ended)
                     {
                         PlayerLogic.MoveLeft = false;
                     }
                 }
             }
         }
     }
 }


PlayerRight.cs

 using UnityEngine;
 using System.Collections;
 
 public class PlayerRight : MonoBehaviour {
 
     // Update is called once per frame
     void Update () 
     {
         //Is there a touch?
         if(Input.touches.Length <= 0)
         {
             //If no touches...
         }
         else //If screen touched..
         {
             //Loop through these
             for(int i = 0; i < Input.touchCount; i++)
             { 
                 //executes this code for current touch
                 if(this.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
                     //If current touch hits the guitexture...
                     if(Input.GetTouch(i).phase ==  TouchPhase.Began)
                     { 
                         PlayerLogic.MoveRight = true;
                     }
                     if(Input.GetTouch(i).phase == TouchPhase.Ended)
                     {
                         PlayerLogic.MoveRight = false;
                     }
                 }
             }
         }
     }
 }


PlayerJump.cs

 using UnityEngine;
 using System.Collections;
 
 public class PlayerJump : MonoBehaviour {
     
     // Update is called once per frame
     void Update () 
     {
         //Is there a touch?
         if(Input.touches.Length <= 0)
         {
             //If no touches...
         }
         else //If screen touched..
         {
             //Loop through these
             for(int i = 0; i < Input.touchCount; i++)
             { 
                 //executes this code for current touch
                 if(this.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
                     //If current touch hits the guitexture...
                     if(guiTexture.HitTest(Input.touches[0].position))
                     {
                         PlayerLogic.Jump = true;   
                     }
                 
                 }
             }
         }
     }
 }


I hope you can understand my question.

Thanks in advance.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MattTheProgrammer · Dec 30, 2013 at 10:36 AM 0
Share

@Phles Thankyou so much it worked I dont know why I didnt realize that when I was checking the code. Probably staying up too late..

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Phles · Dec 30, 2013 at 09:13 AM

Hey there,

In your PlayerJump.cs you are doing 2 HitTest's the first one is checking touches[i] the second nested HitTest is checking touches[0], the second nested HitTest could be causing the problem.

You are looping through touches checking if any touch is touching the guiTexture using GetTouch(i), then with the second nested HitTest you are checking if touches[0] is ALSO touching the guiTexture... this doesn't make sense or I need more sleep.

Try removing the second nested HitTest and see if that helps.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character jumping at random heights. What in my code is causing this? 1 Answer

Simple collision detection C# 2D 1 Answer

Load Scene Moving Too Fast(C#) 1 Answer

2D platformer jumping that feels good 1 Answer

I am having issues with my Jumping Script. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges