Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
0
Question by Eatton · May 22, 2019 at 05:31 PM · physicsrigidbodyaddforcedirection

How to change the direction of a rigidbody without adding to its speed?

I am a newbie trying to making a soccer game with Unity3D. The ball has a rigidbody attached and is moved using AddForce.

I have added a ‘swerve’ effect to the ball by enabling the user to AddForce to the ball with the input keys for one second following each kick. For example, if the ball is kicked directly ‘up’ the screen and then the left key is held down, the ball moves to the left simulating a swerve.

This is working well, but it adds speed to the ball when I only want the input to change the ball’s direction. So if the ball is kicked directly ‘up’ the screen and the ‘up’ key is held down, the ball travels further than I want it to because speed is being added. Ideally I want there to be no effect at all in this scenario, because the direction wouldn't be changed.

So my question is: how can I add direction only to the rigidbody without altering the speed?

Here is how I am adding swerve in FixedUpdate():

 if (shallApplySwerve) // shallApplySwerve is true for 1 second after a kick
 {
     Vector3 inputV3 = new Vector3(matchInputManager.Input.x, 0, matchInputManager.Input.y); // create a Vector3 from input
     Vector3 swerveForce = swerveCapability * inputV3; // swerveCapability is a float to determine how much swerve to add
     rb.AddForce(swerveForce, ForceMode.Impulse);
 }


Any help would be appreciated.

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 Doctor_ED · May 22, 2019 at 07:08 PM

Hmmm. If I understand correctly, then after you kick a ball, you want to be able to alter it's flight with "left" and "right" keys for 1 second.
If so, then instead of making a Vector3, you can just add force to the "side" of the ball :

 [SerializeField]
 private float swerveForce; //some kind of force multiplier, for adjustments
     
  if (shallApplySwerve) // shallApplySwerve is true for 1 second after a kick
 {
 rb.AddForce(rb.transform.right * matchInputManager.Input.x * swerveScale, ForceMode.Impulse);
 }

I'm assuming that matchInputManager.Input.x is just Input.GetAxis("Horizontall")...

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 Eatton · May 22, 2019 at 09:16 PM 0
Share

Thanks for the answer @Doctor_ED but that still increases the velocity of the ball. $$anonymous$$y code already alters the ball flight, but it adds force as well which I don't want. I just want to alter its direction without giving it any extra impetus. I suspect I shouldn't be using AddForce because as the name implies, it adds force. I need something like an 'AddDirection' which wouldn't add any force!

avatar image Doctor_ED · May 23, 2019 at 08:09 AM 0
Share

Well, rigidbody tries to mimic real life physic. If a force is already applied to rigidbody, then it just have some velocity in given direction (vector). In theory, to change it (to be "physically acurate") you should use another force, but...
In unity, you can alter velocity and, for e.g. change it's direction. So to achieve what I think you want to, you can use rb.velocity = rb.transform.forward * rb.velocity.magnitude; on a ball, and then you can use keys, to change only ball rotation. This way the velocity will "follow" the rotation of a ball, giving a, I hope, desired result.

avatar image Eatton Doctor_ED · May 23, 2019 at 03:26 PM 0
Share

I tried this but it didn't work. I added rb.velocity = rb.transform.forward * rb.velocity.magnitude; after each kick and then in FixedUpdate() for one second I did rb.AddTorque(rb.transform.right * matchInput$$anonymous$$anager.Input.x * swerveScale, Force$$anonymous$$ode.Impulse); to change the ball rotation. The ball did not move at all though.

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

270 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 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 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

GetAxis being missed in FixedUpdate work around? 1 Answer

How to make Airplane move forward faster INSTANTANEOUSLY? 1 Answer

AddForce and Momentum‏ 1 Answer

Kinematic sphere doesn't move 1 Answer

How to control the speed of AddForce (ForceMode.Impulse)????? 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