• 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 DrStyles · Oct 03, 2012 at 06:40 AM · javascriptjumpaddforcespherenoob

AddForce to sphere

I have a sphere with rigid body, (Is Kinematic = False) and Configurable Joint (Configured in World Space = True); using AddForce(0, 500, 0). The sphere's movement still depends on it's rotation.

 #pragma strict
 
 var moveSpeedX = 15;
 var maxSpeedX = 15;
 
 var baseSpeed = 0;
 
 var jumpForce1 = 450.00;
 var jumpForce2 = 200.00;
 
 
 private var doubleJump : boolean = true;
 private var grounded : boolean = false;
 
 
 function Update()
 {
     
     if(Physics.Raycast(transform.position, -transform.up, 1)) {
       grounded = true;
       doubleJump = true;
    } else
       grounded = false;
     if(Input.GetButtonDown("Jump")) {
         if(grounded) {
         rigidbody.AddForce(0, jumpForce1, 0);
         }
       else if(doubleJump) {
          doubleJump = false;
          transform.rigidbody.AddForce(0, jumpForce2, 0);
       }
       
         
     }
     
         
     if(Input.GetKey(KeyCode.LeftArrow)) {
         if (transform.rigidbody.velocity.x > -maxSpeedX) {
             transform.rigidbody.velocity.x = transform.rigidbody.velocity.x - (2*moveSpeedX) *Time.deltaTime;
         }  else {
             transform.rigidbody.velocity.x = -maxSpeedX;
         }
     }
     if(Input.GetKey(KeyCode.RightArrow)) {
         if (transform.rigidbody.velocity.x < maxSpeedX) {
             transform.rigidbody.velocity.x = transform.rigidbody.velocity.x + (2*moveSpeedX) *Time.deltaTime;
         }  else {
             transform.rigidbody.velocity.x = maxSpeedX;
         
         }
         
     }
 }
 
 
 

Also tried using: rigidbody.AddForceAtPosition(myForward * 1000.0, rigidbody.worldCenterOfMass); That didn't work either. Sorry for question that a million noobs like me have already asked, but I can't get it to work.

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 JanWosnitza · Oct 03, 2012 at 08:08 AM

rigidbody.AddForce(..) should work as expected. But your raycast should look like t$$anonymous$$s:

 if(Physics.Raycast(transform.position, -Vector3.up, 1))

I also recommend you to use:

 rigidbody.AddForce(0, jumpForce1, 0, ForceMode.VelocityChange);
 rigidbody.AddForce(0, jumpForce2, 0, ForceMode.VelocityChange)

so you can set the speed independently from the sphere's previous velocity (like SuperMario does) and the sphere's mass. Thus you must tweak your jumpForces again.

good luck ;)

Comment

People who like this

0 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 DrStyles · Oct 03, 2012 at 03:26 PM 0
Share

For some reason it didn't move with ForceMode.VelocityChange no matter how I set highjumpForce1, but it seems to work now with: rigidbody.AddForce(Vector3.up*jumpForce1, ForceMode.Acceleration); Thank you.

avatar image JanWosnitza · Oct 04, 2012 at 11:08 AM 0
Share

Your welcome. Maybe ForceMode.Impulse was the better choice since ForceMode.Acceleration is for accelerations (appling a force in every FixedUpdate ^^ e.g. custom gravity for diffent objects)

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

10 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

Related Questions

Help with my double jump script 1 Answer

A sphere moved by relativeforce is acting wierd? 2 Answers

Sphere behaves strange by jumping on diagonal Plattforms or at terrain 0 Answers

Making a character jump with the Character Controller Component instead of Capsule Collider 0 Answers

Punching - what would be the better way? (in javascript) 2 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