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
1
Question by Galatia410 · Mar 17, 2017 at 06:36 AM · transformtimejumptranslate

Problems with jump script

I feel like this is something stupid but i cant figure out whats going wrong with it. My character instantly jumps to peak height(should be a slow jump), then falls slowly as should be. ive tried to put this chunk in both the update and fixed update, same result. i also tried making a new scene with just cube and plane and it still jumps the same. any help would be greatly appreciated, thank you.

Im up for other methods of making my character jump too if this is bad for some reason(imported it from old .js code i was using). Im controlling a clone as a main character and the rigid body and velocity thing isn't working on it either(will not jump at all).

     //Jump
     if (Input.GetButton("Jump"))
     {
          aa = transform.position;
          if (aa.y <= 1)
          {
               bb = transform.position;
               while (bb.y <= (aa.y + 4))
               {
                    transform.Translate(0, (.25f * Time.deltaTime), 0);
                    bb = transform.position;
               }
           }
      }
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 Galatia410 · Mar 17, 2017 at 06:46 AM 0
Share

Sorry should have added that these are declared in the beginning and i have this script attached to my main character.

 static Vector3 aa;
 static Vector3 bb;

1 Reply

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

Answer by Kossuranta · Mar 17, 2017 at 06:56 AM

If you have while, for or foreach loop it will run completely before code can continue. In your case code will be stuck in while until (bb.y

You said that he falls slowly which sounds like you are using physics and rigidbody? If this is the case then you could just use force something like this. I didn't test any of these examples, but they should at least give you the idea how to solve your problem.

 public float jumpForce;
 public Rigidbody rb;

 void Start() 
 {
     rb = GetComponent<Rigidbody>();
 }

 void FixedUpdate() 
 {
     if (Input.GetButton("Jump"))
     {
         rb.AddForce(transform.up * jumpForce);
     }
 }



If for some reason you want or need to use Translate then you can use coroutine or just do it in update without any loops. In Update something like this should work.

 if(isJumping)
 {
     if(bb.y <= (aa.y + 4))
     {
         transform.Translate(0, (.25f * Time.deltaTime), 0);
         bb = transform.position;
     }
     else
     {
         isJumping = false;
     }
 }
 else if (Input.GetButton("Jump"))
 {
     aa = transform.position;
     if (aa.y <= 1)
     {
         bb = transform.position;
         isJumping = true;
     }
 }



Or then with coroutine:

 void Update()
 {
     if (Input.GetButton("Jump"))
     {
         aa = transform.position;
         if (aa.y <= 1)
         {
             bb = transform.position;
             StartCoroutine(Jump());
         }
     }
 }
 
 IEnumerator Jump()
 {
     while (bb.y <= (aa.y + 4))
     {
         transform.Translate(0, (.25f * Time.deltaTime), 0);
         bb = transform.position;
         yield return new WaitForEndOfFrame();
     }
 }

Comment
Add comment · Show 1 · 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 Galatia410 · Mar 17, 2017 at 07:03 AM 0
Share

Thank you very much, that was extremely helpful :) going back to work on a game i had almost completed in unity 3... lol kinda forgot a lot and things changed ;)

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

73 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

Related Questions

transform.Translate and transform.eulerAngles bug 0 Answers

Gradually scale platform? 4 Answers

Tranform.translate does not have constant speed 1 Answer

transform.Translate tutorial question 2 Answers

how to apply translate to gameobject? 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