Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by $$anonymous$$ · Jan 30, 2017 at 07:34 AM · jumpingempty game object

Create jumping motion with only transform.Translate

I have an empty 3D GameObject that is serving as the player in a game that I am building. I am trying to write a script that enables the player to "jump" when SPACE is pressed. I want to do this using kinematic equations, and with only transform.Translate. Here is the code I have so far:

 public float fallingSpeed;

 void Update () {
     if (Input.GetKey (KeyCode.Space)) {
         Jump ();
     }
 }

 public void Jump () {
     float yPosition = 0.5f;

     // Don't let player fall through floor
     if (transform.position.y <= 0.5f) {
         fallingSpeed = 0.0f;
     } else {
         fallingSpeed -= 9.8f * Time.deltaTime;
         yPosition += fallingSpeed * Time.deltaTime;
     }

     // Translate y-position
     transform.Translate (new Vector3 (0.0f, yPosition, 0.0f));
 }

Currently, when SPACE is pressed, the player launches into the air and doesn't come back down. I have tried tinkering with the logic for some time, but can't seem to wrap my head around why it's not working.

Any help is appreciated!

EDIT: it seems as though my player reacts well to the mechanics I have set up... when SPACE is held down. As soon as you let go, the player hangs at whatever y-position he is currently situated, and doesn't move until you press SPACE again. How do I get him to keep falling even when I let go of SPACE? Will that have to be in the Update() method? Thanks!

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 david-1495 · May 19, 2018 at 03:09 PM

one word : rigidbody

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 jonatiti48 · May 28, 2019 at 07:04 AM 0
Share

Each rigidbody takes computation, so less are of course ideal. Rigidbodies also have the ability to sleep when their angular and translational velocity drop below a certain threshold. When this happens, the amount of computation they require drops significantly and remains low until they have a force manually applied to them or a collider touches their collider if it exists. https://wiki.unity3d.com/index.php/General_Performance_Tips

Sometimes is better to avoid rigidbody when possible

avatar image Caino1797 · Apr 24 at 09:31 AM 0
Share

The solution is simple. You have:

  void Update () {
      if (Input.GetKey (KeyCode.Space)) {
          Jump ();
      }

And in your Jump function is:

      } else {
          fallingSpeed -= 9.8f * Time.deltaTime;
          yPosition += fallingSpeed * Time.deltaTime;
      }

Your falling logic is in the bottom half of your jump script but you're only calling it if the SPACE button is being pressed. Try putting your falling logic in a different function entirely and call Fall() if you are above the floor.

  public float fallingSpeed;
 
 private  void Update () {
      if (Input.GetKey(KeyCode.Space)) {
          Jump ();
      }
      Fall();
  }
 
  public void Jump () {
      float yPosition = 0.5f;
  }
 
  void Fall() {
      if (transform.position.y <= 0.5f) {
          fallingSpeed = 0.0f;
      } else {
          fallingSpeed -= 9.8f * Time.deltaTime;
          yPosition += fallingSpeed * Time.deltaTime;
      }

      // Translate y-position
      transform.Translate (new Vector3 (0.0f, yPosition, 0.0f));
  }

I hope this helped :)

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

91 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

Related Questions

vibrating jumps and collisions 0 Answers

Touch control swipe up to jump 1 Answer

My game seems to be running too fast for certain tasks 0 Answers

problem with jump script 2 Answers

Need help with adding ground detection to my jump. (c#) (3D) 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