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 niick555 · Sep 29, 2015 at 09:09 AM · androidtouchjumptouchscreenswipe

Problem with swipe and player jump

Good night, all right? I'm programming a mobile game and I'm the problem: I have a code to check if the player makes the "swipe" on the screen up to perform a leap of character. The code works correctly, however, the size of the leap varies according to swipe the size of the player did, how can I adjust so that, regardless of size, the jump is the same height (I set the AddForce)?

 if(Input.GetTouch(0).phase == TouchPhase.Moved) {
     var posicaoDeltaTouch : Vector2 = Input.GetTouch(0).deltaPosition;
 
     if(posicaoDeltaTouch.y > 5 && posicaoDeltaTouch.x < 5 && posicaoDeltaTouch.x > -5 && jogador.GetComponent(ColisaoPlayer).naSuperficie == true) {
         jogador.GetComponent(Rigidbody).AddForce(0, 400, 0);
     }
 }

Thank you!

Comment
Add comment · Show 2
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 Denvery · Sep 29, 2015 at 09:32 AM 0
Share

Sorry, I can't understand, is your jump now depends on swipe size or not? it seems not, because i in AddForce(0, 400, 0) 400 is fixed number

avatar image niick555 Denvery · Sep 29, 2015 at 09:41 AM 0
Share

Currently the jump is depending on the swipe size also can not understand why, since they define a specific value. The jump is not to depend on the size swipe, in case if the player makes a swipe greater than the set, the jump must be the same size.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Rostam24 · Sep 29, 2015 at 10:13 AM

It's probably being called several times. Place a debug.log() right where you make the character jump to check if this is indeed happening. If so, you'll have to add another check so that players can't jump twice with 1 swipe.

Comment
Add comment · Show 3 · 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 Rostam24 · Sep 29, 2015 at 11:24 AM 0
Share

@niick555: checking if the character on the ground might not be good enough, as a quick / big swipe might cause several 'jumps' before the character is in the air. Test if this is happening by debugging as I mentioned earlier. And if so, you'll need an extra check. For instance, a bool 'firstJump' that is set to false after jumping, and is set to true when touchphase is began.

avatar image niick555 Rostam24 · Sep 29, 2015 at 07:26 PM 0
Share

I auditioned and is really going on several jumps. Tried to make firstJump variable as you said, but I believe I did wrong, why did not solve the problem ... Can you help? All of the code below is within the Update (). The variable declaration is outside the Update ().

 if(Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
     var posicaoDeltaTouch : Vector2 = Input.GetTouch(0).deltaPosition;
     firstJump = true;
 
     if(posicaoDeltaTouch.y > 5 && posicaoDeltaTouch.x < 5 && posicaoDeltaTouch.x > -5 && jogador.GetComponent(ColisaoPlayer).naSuperficie == true && firstJump == true) {
         jogador.GetComponent(Rigidbody).AddForce(0, 400, 0);
         primeiroPulo = false;
         Debug.Log("Jump");
     }
 }

Thank you!

avatar image Rostam24 niick555 · Sep 29, 2015 at 08:59 PM 0
Share

Give this a try: if(Input.GetTouch(0).phase == TouchPhase.Began) firstJump = true;

  if(Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
      var posicaoDeltaTouch : Vector2 = Input.GetTouch(0).deltaPosition;
  
      if(posicaoDeltaTouch.y > 5 && posicaoDeltaTouch.x < 5 && posicaoDeltaTouch.x > -5 && jogador.GetComponent(Rigidbody).naSuperficie  && firstJump) {
          jogador.GetComponent(Rigidbody).AddForce(0, 400, 0);
          firstJump = false;
      }
  }
 

The code looks a bit weird, as not all of it is in the same language - but I'm sure you can make the necessary corrections to make it work :)

avatar image
0

Answer by niick555 · Sep 29, 2015 at 11:19 AM

@Rostam24 I understand, but it turns out that there is already a variable "naSuperficie" that makes the player jump only when it is on the ground, that is, you can not make another jump while it is in the air. I do not understand what may be happening ...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to optimize touch input script 0 Answers

Jump by touch Unity C# 0 Answers

How to replace KeyCode.Space with a simple tap on Android? 1 Answer

Touch on 2D gameobject: should i use a GUI button? 1 Answer

How do I transfer the control to the PC on your android? how to change the script the character animation that would work? what should be done? 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