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
0
Question by JarPet · Jul 31, 2011 at 09:51 AM · rigidbodyaddforcedirection

AddForce with direction tuning

Hello everybody. I´m using this script for moving my Rigidbody Capsule Character.

 void FixedUpdate () {
     //Only move if on a solid ground
     if (onGround && collisionAngle > slopeAngle) {
         //Get desired velocity
         velDesired = new Vector3( Input.GetAxis("Horizontal"), 0.0F, Input.GetAxis("Vertical") );
             velDesired = transform.TransformDirection(velDesired);
         speed = walkSpeed * ( bool2Int( Input.GetKey(KeyCode.LeftShift) )+1 );
             velDesired *= speed;
         //Get current velocity
         velCurrent = rigidbody.velocity;
         //Get the difference in between
         velDifference = velDesired - velCurrent;
             velDifference.y = 0.0F;
         //Apply the difference to body 
         rigidbody.AddForce(velDifference, ForceMode.VelocityChange);
         //Get if want to jump
         if ( Input.GetButton("Jump") ) {
             //float velJump = Mathf.Sqrt(2.0F * jumpHeight * gravity);
             velCurrent = rigidbody.velocity;
             rigidbody.velocity = new Vector3(velCurrent.x, velJump, velCurrent.z);
             //onGround = false;
         }
     }
     else {
         rigidbody.AddForce( new Vector3(0.0F, -gravity * rigidbody.mass, 0.0F) );
     }
 }

Everything is fine, I´d like to tune it a bit. I remove "onGround" check in the first if, because I want to move it in the air as well. It works, but if I addForce and a vector which is a direction to my Rigidbody by another collider when I´m in air, I can´t move it anymore. Does anyone know where the problem is, please? :)

Thanks JarPet

Comment
Add comment · Show 4
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 aldonaletto · Jul 31, 2011 at 12:30 PM 0
Share

This isn't clear: but if I addForce and a vector which is a direction to my Rigidbody by another collider when I´m in air. The character is flying, ok, but the other factors that cause the issue are very obscure. You're trying to change direction, collides with something and the character gets stuck, is it? And it doesn't happen when you're on the ground?

avatar image C4sh · May 18, 2012 at 03:38 PM 0
Share

Hi, I think I'm having a similar problem, I'm trying to shoot a bullet and it's standing on the air without moving when I use bullet.rigidbody.addForce(direction);

but that works if the bullet is grounded (the ball moves on rolling on the ground).

What I want is to move the bullet on the air (freezing the rigidbody in y-position) but it doesn't work then.

Can anyone help?

avatar image Owen-Reynolds · May 18, 2012 at 04:03 PM 0
Share

To follow up: the script above is working? The problem is when you make some changes that we don't know about?

One cheesy sidestep can be to (temporarily) turn off useGravity.

avatar image C4sh · May 18, 2012 at 04:12 PM 0
Share

Just made it work with bullet.rigidbody.AddForce(bullet.transform.forward*250), freezing y-position.

Don't know exactly why this got so easily solved oO

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is force only being added in the same direction? 1 Answer

AddForce on Object at Direction of Click 2 Answers

throw object at specific direction or angle 2 Answers

Rigidbody.AddTorque/AddForce Question. 1 Answer

How can I convert velocity/direction to Force? 3 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