Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 wolis · Oct 21, 2011 at 11:01 AM · 2djump

Accelerating speed

HI guys. I am creating a 2d game with a spaceship going up. The problem I am having is that in stead of keeping flying up while I press up, if gravity is enabled on rigidbody (which I want to be that way) The ship "jumps" instead of flying for a couple of meters and then falls down. I guess I need to code a speed acceleration, but going through different examples I can`t manage to grasp how to do it. I know it is unpolite to ask for someone to code for me, but if you could help with some advice for such a supernoob as me, I would really apprciate it.

What I have so far:`

{ public float PlayerSpeed;

 // Update is called once per frame
 void Update () 
 {
     //amount to move
     float amtToMove = Input.GetAxisRaw("Horizontal") * PlayerSpeed * Time.deltaTime;
     float amtToJump = Input.GetAxisRaw("Vertical") * PlayerSpeed * Time.deltaTime;
     
     //move the player
     transform.Translate(Vector3.right * amtToMove);
     transform.Translate(Vector3.up * amtToJump);
     //accelerate

 }

}`

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 syclamoth · Oct 21, 2011 at 11:02 AM 0
Share

Just use rigidbodies for this, it's way easier. Then, just make sure that you're using enough power in AddForce to overcome gravity, and you're all good!

2 Replies

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

Answer by DavidDebnar · Oct 21, 2011 at 02:22 PM

Use rigidbody.AddForce(); function instead.

 { 
     public float PlayerSpeed;
 
     void Update () 
     {
         float amtToMove = Input.GetAxisRaw("Horizontal") * PlayerSpeed * Time.deltaTime;
 
         transform.Translate(Vector3.right * amtToMove);
         transform.Translate(Vector3.up * amtToJump);
         if(Input.GetAxisRaw("Vertical") == 1)
         {
            rigidbody.AddForce(transform.up * PlayerSpeed);
         }
     
     }
 }

  • David

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 wolis · Oct 21, 2011 at 02:49 PM 0
Share

This works exactly how I wanted it to be! Thanksm, $$anonymous$$! ^__^

avatar image DavidDebnar · Oct 21, 2011 at 03:02 PM 0
Share

No problem :).

avatar image syclamoth · Oct 22, 2011 at 12:34 AM 0
Share

This... isn't a very good solution. $$anonymous$$ixing transform.Translate with rigidbody movement is pretty dangerous.

avatar image
1

Answer by syclamoth · Oct 21, 2011 at 11:07 AM

Do you have a rigidbody on your object? If so, you should never really use transform.Translate for anything- instead, use Rigidbody.AddForce. In your case, that would look more like-

 float thrust = Input.GetAxisRaw("Vertical") * accelerationPower * Time.deltaTime;
 // Spaceships only fire their rockets one way-
 thrust = Mathf.Abs(thrust);
 rigidbody.AddForce(transform.up * thrust);

Then just tweak accelerationPower until it feels right! For the other axis, depending on what you want to do, you would either use rigidbody.AddForce as here, or in fact rigidbody.AddTorque if you want to change your rocket's rotation.

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 syclamoth · Oct 21, 2011 at 11:08 AM 0
Share

In a pinch, rigidbody.$$anonymous$$ovePosition will work, but it's not fantastic, and physics doesn't like you very much when you do that.

avatar image wolis · Oct 21, 2011 at 02:00 PM 0
Share

Hi! Thanks for your answer! I tried it, the console shows no errors, but the object is now not moving at all and I have no idea why. Is there something else that has to be switched on? So far i have done everything with transform.translate, so I am new this (sorry :) )

avatar image syclamoth · Oct 22, 2011 at 12:34 AM 0
Share

You need to increase the force until it makes a difference! Presumably, your accelerationPower isn't high enough to overcome gravity.

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

Character doesn't jump repeatedly 1 Answer

Don't jump continuously when button held down 0 Answers

Jumping from a floor to another floor (2D) 2 Answers

how to jump at fixed height but faster 0 Answers

How to fix Jump problem with 2d Platformer Unity demo game? 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