Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by FIZZZGAR · Apr 16, 2019 at 06:16 PM · c#jumpingtouch controlstouchscreen

Pressing Vs Jumping

Hello, I'm still fairly new to unity and following tutorials in order to learn/improve. In one of the tutorials, a script was made in order to change jump height (higher or lower) depending on if the user pressed or held down the button. It works if I use the space bar, but when I use the touch screen on my computer or click with the mouse it does the same jump height each time. How would one make this work for touch screen ex. for a mobile game where you might not have access to buttons.I thought it would be easier if you can see my script so I added it. Also i was thinking that if i can't figure out the issue with the touchscreen then i would create a button that basically acted like a space bar for when the game is played on a mobile device.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;

public class PlayerContol : MonoBehaviour { public float moveSpeed; private float moveSpeedStore; public float speedMultiplier;

 public float milestone;
 private float speedIncreaseMileStoneStore;

 private float milestoneCount;
 private float speedMilestoneCountStore;

 public float jumpForce;

 public float jumpTime;
 private float jumpTimeCount;

 private bool stoppedJumping;
 private bool canDoubleJump;

 private Rigidbody2D myRigidbody;

 public bool grounded;
 //private bool isFalling;
 public LayerMask whatIsGround;
 public Transform groundCheck;
 public float groundCheckRad;

 public GameManger theGameManager;

 //private Collider2D myCollider;

 private Animator myAnimator; 

 // Start is called before the first frame update
 void Start()
 {
     myRigidbody = GetComponent<Rigidbody2D>();
     //myCollider = GetComponent<Collider2D>();

     jumpTimeCount = jumpTime;

     milestoneCount = milestone;

     moveSpeedStore = moveSpeed;
     speedMilestoneCountStore = milestoneCount;
     speedIncreaseMileStoneStore = milestone;

     stoppedJumping = true;

     myAnimator = GetComponent<Animator>();
 }

 // Update is called once per frame
 void Update()
 {
     //grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
     //grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
     grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRad, whatIsGround);

     if(transform.position.x > milestoneCount)
     {

         milestoneCount += milestone;

         milestone = milestone * speedMultiplier;
         moveSpeed = moveSpeed * speedMultiplier;

         speedMilestoneCountStore = milestone;

     }

     myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);

     if /*(Input.GetButtonDown("Jump"))*/  (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
     {
         //prevents jump on pause
         if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject == null)
             return;
         if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
         {
             if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
                 return;
         }

         //jump code
     
         if (grounded)
         {

             myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
             stoppedJumping = false;//false
         }

         if (!grounded && canDoubleJump)
         {
             myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
             jumpTimeCount = jumpTime;
             stoppedJumping = false;
             canDoubleJump = false;//false

         }
     }
     
     if/*(Input.GetButtonDown("Jump"))*/((Input.GetKey(KeyCode.Space) || Input.GetMouseButtonDown(0)) && !stoppedJumping)
     {
         //prevents jump on pause
         if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject == null)
             return;
         if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
         {
             if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
                 return;
         }

         //jump code

         if (jumpTimeCount > 0)
         {

             myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
             jumpTimeCount -= Time.deltaTime;

         }
     }

     if /*(Input.GetButtonUp("Jump"))*/(Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0) )
         {

         //prevents jump on pause
         if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject == null)
             return;
         if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
         {
             if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
                 return;
         }

         //jump code
         jumpTimeCount = 0;
             stoppedJumping = true;

         }

     if (grounded)
     {

         jumpTimeCount = jumpTime;
         canDoubleJump = true;
             
     }

     myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
     myAnimator.SetBool("Grounded", grounded);

 }

 void OnCollisionEnter2D(Collision2D collision)
  {
         if (collision.gameObject.tag == "KillBox")
         {
                
                theGameManager.RestartGame();
                moveSpeed = moveSpeedStore;
                milestoneCount = speedMilestoneCountStore;
                milestone = speedIncreaseMileStoneStore;
         }
  
  }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KevRev · Apr 16, 2019 at 07:37 PM

GetMouseButtonDown triggers once on click, but GetMouseButton is true whilst the button is held.

In the first if check, try:

 Input.GetMouseButton(0)

Then instead of using Input.GetMouseButtonUp(0):

 If (!Input.GetMouseButton(0))
Comment
Add comment · Show 2 · 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
avatar image FIZZZGAR · Apr 17, 2019 at 12:06 AM 0
Share

Awesome thank you it's working now.

avatar image KevRev · Apr 17, 2019 at 06:42 AM 0
Share

Excellent. Glad I could help :)

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

667 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ScreenToWorldPoint on One Axis? 0 Answers

Unity 2d android game How to "freeze" position on Y axis 1 Answer

Cannot Jump while moving, using Touch Controls for Android 1 Answer

OnScreen-Stick that moves to the position touched 0 Answers

Touch control swipe up to jump 1 Answer


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