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 CoreApp2 · Sep 17, 2015 at 06:40 PM · androidtouchvelocityjumpheight

Touch the screen and jump two time with the same height

Hello,

Please i have a problem, that i cant solve since a few days. I want to make that a ball jump two time. First when we touch the screen. If the ball is grounded, it can jump and the second time when it is in the air. and with the same height.

This is my code, what i tried

      void update {
 if (Physics.Raycast(transform.position, -Vector3.up, raycastDistance))
                 {
                     isGrounded = true;
                     airCount = maxAirCount;
                 }
                 else
                 {
                     isGrounded = false;
                 }
         
                 if (isGrounded)
                 {
                     rb.velocity = Vector3.right * 190f * Time.deltaTime;
                 }
         
                 //Für Touch
                 if (Input.touches.Length > 0)
                 {
                     for (int i = 0; i < Input.touchCount; i++)
                     {
                         if ((Input.GetTouch(0).phase == TouchPhase.Began))
                             if (isGrounded)
                             {
                                 rb.velocity = new Vector3(200f * Time.deltaTime, rb.velocity.y + normalJumpForce * Time.deltaTime, 0);
                             }
                             else if (airCount > 0)
                             {
                                 airCount -= 1;
                                 rb.velocity = new Vector3(200f * Time.deltaTime, rb.velocity.y + normalJumpForce * Time.deltaTime, 0);
                             }
                     }
                 }
 }



The ball moves to the right. The height varies always. Please i need your help !!

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

Answer by sonu124 · Sep 18, 2015 at 10:31 AM

Try this script.....

using UnityEngine; using System.Collections;

public class BallJump : MonoBehaviour {

 private CharacterController controller;
 private Vector3 force;
 private int count =0;
 private bool isGrounded;
 // Use this for initialization
 void Start ()
 {
     controller = this.GetComponent<CharacterController> ();
     force = new Vector3 (0f,250f,0f);
 }
 
 // Update is called once per frame
 void Update () 
 {
     MouseInput ();
     if(isGrounded)
     {
         count =0;
     }
 }
 void OnCollisionStay(Collision col)
 {
     if(col.transform.tag == "Ground")
     {
         isGrounded =true;
     }
 }
 private void MouseInput()
 {
     if(Input.GetMouseButtonDown(0))
     {

         count++;
         if(count <2)
         {
             isGrounded = false;
             rigidbody.velocity = Vector3.zero;
             rigidbody.AddForce(force);
         }
     }
     else if(Input.GetMouseButtonUp(0))
     {
         gameObject.rigidbody.useGravity = true;
     }
 }

}

Comment
Add comment · Show 5 · 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 CoreApp2 · Sep 19, 2015 at 12:55 AM 0
Share

thanks for the reply. It seems to work but not when you add for example a force, so the ball can move alone to the right, for example if you add addforce(vector3.right * 10f); and after that try to jump, the height will not be the same, anymore.

avatar image sonu124 · Sep 19, 2015 at 09:28 AM 0
Share

Try to apply both forces on your gameobject.. rigidbody.AddForce(Vector3.up) & rigidbody.AddForce(Vector3.right*500f)

public class BallJump : $$anonymous$$onoBehaviour {

 private CharacterController controller;
 private int count =0;
 private bool isGrounded;
 // Use this for initialization
 void Start ()
 {
     controller = this.GetComponent<CharacterController> ();
 }
 
 // Update is called once per frame
 void Update () 
 {
     $$anonymous$$ouseInput ();
     if(isGrounded)
     {
         count =0;
     }
 }
 void OnCollisionStay(Collision col)
 {
     if(col.transform.tag == "Ground")
     {
         isGrounded =true;
     }
 }
 private void $$anonymous$$ouseInput()
 {
     if(Input.Get$$anonymous$$ouseButtonDown(0))
     {

         count++;
         if(count <2)
         {
             isGrounded = false;
             rigidbody.velocity = Vector3.zero;
             rigidbody.AddForce(Vector3.up*500f);
             rigidbody.AddForce(Vector3.right*500f);
         }
     }
     else if(Input.Get$$anonymous$$ouseButtonUp(0))
     {
         gameObject.rigidbody.useGravity = true;
     }
 }

}

avatar image CoreApp2 · Sep 19, 2015 at 09:35 PM 0
Share

Already at the start, the ball is moving to the right alone, so we must have something like that

     void Update () 
      {
          rigidbody.AddForce(Vector3.right*500f);
          $$anonymous$$ouseInput ();
          if(isGrounded)
          {
              count =0;
          }
      }

when i try your code, the ball is not moving alone to right direction, but only when the mouse button is down and if you add addforce like it is in the above code, the height will not be the same at the jump.

avatar image sonu124 CoreApp2 · Sep 21, 2015 at 04:30 AM 0
Share

Try this one. i hope ,it will help you.

public class BallJump : $$anonymous$$onoBehaviour {

 private CharacterController controller;
 private int count =0;
 private bool isGrounded;
 // Use this for initialization
 void Start ()
 {
     controller = this.GetComponent<CharacterController> ();
 }
 
 // Update is called once per frame
 void Update () 
 {
     rigidbody.AddForce(Vector3.right*25f);
     $$anonymous$$ouseInput ();
     if(isGrounded)
     {
         count =0;
     }
         
 }
 void OnCollisionStay(Collision col)
 {
     if(col.transform.tag == "Ground")
     {
         isGrounded =true;
     }
 }
 private void $$anonymous$$ouseInput()
 {
     if(Input.Get$$anonymous$$ouseButtonDown(0))
     {

         count++;
         if(count <2)
         {
             isGrounded = false;
             rigidbody.velocity = Vector3.zero;
             rigidbody.AddForce(Vector3.up*500f);
             rigidbody.AddForce(Vector3.right*500f);
             count++;
         }

     }
     else if(Input.Get$$anonymous$$ouseButtonUp(0))
     {
         gameObject.rigidbody.useGravity = true;
     }
 }

}

avatar image CoreApp2 sonu124 · Sep 23, 2015 at 09:33 PM 0
Share

hello, ok thanks for reply

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObject won't jump with touch 1 Answer

How to add Jump control from touch on screen? ANDROID 1 Answer

Android issues, jump force not consistent, touch not consistent 0 Answers

how to add a speed limit? 1 Answer

Fixed Jump on android phone 0 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