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 /
avatar image
0
Question by mpal · Nov 15, 2016 at 09:59 AM · jumping object

swipe player to up jump.

This is probably extremely simple but bare with me i'm new to touch inputs. I'm creating a game for android devices where when the player swipes up the character jumps. Any insight on how to achieve this?

For that I used one link code:-

void Update() { #if UNITY_ANDROID || UNITY_IPHONE if (Input.touchCount > 0) { Touch touch = Input.touches[0];

     switch (touch.phase)
     {
         case TouchPhase.Began:
             startPos = touch.position;
             StartCoroutine(Jump());
             break;
         case TouchPhase.Moved:
             isSwipe = true;
             float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
             float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
             if (swipeDistHorizontal > minSwipeDistX)
             {
                 float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
                 if (swipeValue > 0 && !isTouch)//to right swipe
                 {
                     isTouch = true;
                     StartCoroutine(Right());
                 }
                 else if (swipeValue < 0 && !isTouch)//to left swipe
                 {
                     isTouch = true;
                     StartCoroutine(Left());
                 }
             }

             //add swipe to up
             if(swipeDistVertical > minSwipeDistY)
             {
                 float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
                 if(swipeValue > 0 && !isTouch)
                 {
                     isTouch = true;
                     StartCoroutine(Jump2());
                 }
             }
             break;
         case TouchPhase.Ended:
             isSwipe = false;
             isTouch = false;
             break;
     }
 }
 #endif

} IEnumerator Jump2() { yield return new WaitForSeconds(0.05f); if(playerVelocity <= 0.2f) { Debug.Log("Swipe Up"); } }

IEnumerator Jump() { if (!isSwipe) { yield return new WaitForSeconds(0.05f); if (!isSwipe && playerVelocity <= 0.2f) { Debug.Log("Tap");

     }
     else
     {
         yield break;
     }
 }
 else
 {
     yield break;
 }

}

IEnumerator Right() { Debug.Log("Right");

}

IEnumerator Left() { Debug.Log("Left");

}

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
1

Answer by Batka · Nov 15, 2016 at 11:13 AM

Here is how I did it, but using Character Controller

If you are using real life physics, this is not very useful.

 private Vector3 moveDirection = Vector3.zero;
 private CharacterController cc;
 public float gravity = 9.81f;
 
 void Update() {
 
 if (Input.touches.Length > 0)
             {
                 Touch t = Input.GetTouch(0);
                 if (t.phase == TouchPhase.Began)
                 {
                     //save began touch 2d point
                     firstPressPos = new Vector2(t.position.x, t.position.y);
                 }
                 if (t.phase == TouchPhase.Ended)
                     {
                     //save ended touch 2d point
                     secondPressPos = new Vector2(t.position.x, t.position.y);
     
                     //create vector from the two points
                     currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
     
                     //normalize the 2d vector
                     currentSwipe.Normalize();
     
                     //swipe up
                     if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f && _characterController.isGrounded)
                     {
                         moveDirection.y = jumpSpeed;
                         
                     }

         //make the character fall each update
         moveDirection.y -= gravity * Time.deltaTime;
         moveDirection.z = zVelocity;
         _characterController.Move(moveDirection * Time.deltaTime);
 
 }



I hope it leads you somewhere safer :P

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

i want to make a ball jump 1 time please can i have someones script? 0 Answers

Jump over object help 1 Answer

Character not jumping 1 Answer

Why is enemy jumping at different speed even though the velocity is the same? 0 Answers

How can I make an object in my scene jump continuously? 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