• 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
Question by LinkUpGames · Sep 19, 2014 at 04:24 PM · javascriptrigidbodyforcetime.deltatime

How to AddForce OverTime?

Hi there, I wanted to add into a FPS the ability to use lift like found in Bungie's game Destiny, I was able to get a second jump to occur after leaving the ground but I can't get it to sustain the added force for a given time. Here's my code:

 //Walking variables
 var walkAcceleration : float = 5;
 var walkDeacceleration : float = 5;
 var walkAccelAirRatio : float = 0.3;
 @HideInInspector
 var walkDeaccelerationVolX : float;
 @HideInInspector
 var walkDeaccelerationVolZ : float;
 var cameraObject : GameObject;
 var maxWalkSpeed : float = 20;
 @HideInInspector
 var horizontalMovement : Vector2;
 
 //Jumping variables
 var jumpVelocity : float = 40;
 @HideInInspector
 var grounded : boolean = false;
 var maxSlope : float = 60;
 
 //Lift variables
 var liftVelocity : float = 20;
 var liftDuration : float = 3;
 var liftCDTime : float = 5;
 var usingLift : boolean = false;
 
 function Start () 
 {
 
 }
 
 function Update () 
 {
     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
     if(horizontalMovement.magnitude > maxWalkSpeed)
     {
         horizontalMovement.Normalize();
         horizontalMovement *= maxWalkSpeed;
     }
     rigidbody.velocity.x = horizontalMovement.x;
     rigidbody.velocity.z = horizontalMovement.y;
     
     if(grounded)
     {
         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolX, walkDeacceleration);
         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolZ, walkDeacceleration);
         liftDuration = 3;
         usingLift = false;
     }
 
     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
     
     if(grounded)
     rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
     else
     rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio * Time.deltaTime);
     
     if(Input.GetButtonDown("Jump") && grounded)
         rigidbody.AddForce(0, jumpVelocity, 0);
 
     if(Input.GetButtonDown("Jump") && !grounded)
         {
         usingLift = true;
             if(liftDuration > 0)
                 {
                     rigidbody.AddForce(0, liftVelocity * liftDuration, 0);
                 }
         }
     
     if(usingLift)
     liftDuration -= Time.deltaTime;
 }
 
 function OnCollisionStay (collision : Collision)
 {
     for (var contact : ContactPoint in collision.contacts)
     {
         if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
         {
             grounded = true;
         }
     }
 }
 
 function OnCollisionExit ()
 {
     grounded = false;
 }

It counts down the liftDuration variable properly but aside from the initial force exerted when pressing the jump key it isn't sustained so I can't make it seem like you are hovering using a thruster or jump pack. Any ideas on how I can fix this?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by dmg0600 · Sep 19, 2014 at 05:13 PM

The problem is you are using Input.GetButtonDown("Jump") for the lift effect instead of Input.GetButton("Jump").

Input.GetButtonDown only triggers at the exact time the key is pressed while Input.GetButton triggers every frame the key is maintined.

Change the line 60 from if(Input.GetButtonDown("Jump") && !grounded) to if(Input.GetButton("Jump") && !grounded)

Other than that you just have to adjust the lift velocity according to the mass of the rigidbody and the lift velocity that you want.

Comment

People who like this

0 Show 4 · 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 LinkUpGames · Sep 19, 2014 at 05:44 PM 0
Share

Wouldn't that make it so lift only occurs when holding the jump button? I want it to be trigger that occurs when you press the jump key for the allotted time.

avatar image dmg0600 · Sep 19, 2014 at 05:53 PM 0
Share

Ok, I missunderstood. To do so you have to move the rigidbody.AddForce from the if.

 if(Input.GetButtonDown("Jump") && !grounded)
 {
     usingLift = true;
 }
  
 if(usingLift && liftDuration > 0)
 {
     liftDuration -= Time.deltaTime;
 
     rigidbody.AddForce(0, liftVelocity/* * liftDuration*/, 0);
 }

That way when you press jump when in the air, the lift effect is activated by setting the variable usingLift to true.

While it is true and the liftDuration hasn't come to 0, it will reduce the liftDuration and apply the force.

avatar image LinkUpGames · Sep 19, 2014 at 06:06 PM 0
Share

Thanks! Worked perfectly ^-^

avatar image dmg0600 · Sep 19, 2014 at 06:17 PM 0
Share

Glad to hear that! Please mark it as answered. If you have further questions, don't hesitate in asking.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Adding force to rigidbody 1 Answer

About Forces and Time.deltaTime 3 Answers

How to set max speed for this car ? 1 Answer

How to make an object wait until it finishes moving? 0 Answers

How can I determine force and direction by holding mouse? 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