• 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 Wolfdog · Sep 25, 2015 at 10:08 PM · scripting problemphysicsrigidbody

Rigidbody set velocity on 1 axis without changing the others.

What I basically want is to modify the transform.forward velocity if something. I want to keep the down velocity resulting from gravity and a side velocity resulting from wind. All i need is to set the forward velocity.

The script below however resets the whole velocity, so gravity no longer works.

 void FixedUpdate() {
     r.velocity = transform.forward * targetSpeed;
 }

To put it into context, I have a fighter jet which accelerates and decelerates. I control the target speed and I don't wish to rely on addforce().

Is there a way by which I can update the forward velocity every fixed update, but also keep the up and right velocities?

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

5 Replies

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

Answer by rutter · Sep 26, 2015 at 12:10 AM

If "forward" is always pointing along the z-axis, then it's pretty straightforward to grab the velocity and change that one axis:

 Vector3 vel = rigidbody.velocity;
 vel.z = 0f;
 rigidbody.velocity = vel;

If "forward" can be any direction, then you may need to apply a concept called vector projection. The jet's total velocity can be divided into three components, all based on the jet's local axes.

 //get the jet's velocity, break it into three parts
 //each part represents the jet's world-space velocity in terms of its local axes
 Vector3 vel = rigidbody.velocity;
 Vector3 jetUp = Vector3.Project(vel, transform.up);
 Vector3 jetRight = Vector3.Project(vel, transform.right);
 Vector3 jetFwd = Vector3.Project(vel, transform.forward);
 
 //separate the direction and speed
 Vector3 fwdDir = jetFwd.normalized;
 float fwdSpeed = jetFwd.magnitude;
 
 //let's say we want to cap the jet's forward speed between 1 and 5
 fwdSpeed = Mathf.Clamp(fwdSpeed, 1f, 5f);
 
 //recombine direction and speed
 jetFwd = fwdDir * fwdSpeed;
 
 //recombine the three component vectors
 vel = jetUp + jetRight + jetFwd;
 rigidbody.velocity = vel;

The above code is effectively doing the same thing as the first example, with some added math to remove the assumption that you're traveling straight down the z-axis.

Comment
Add comment · Show 1 · 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 LukeNukem44 · Jan 06, 2019 at 01:09 AM 0
Share

Wrong.

Your solution still cancels the other axis' forces.

avatar image
2

Answer by Glurth · Sep 26, 2015 at 12:34 AM

Why not do it the way nature does, and apply air resistance, in the opposite direction of movement? The strength of the air-resistance force, increases based upon the speed squared.

(When this force is equal to the force of gravity/ or your engines, you have reached terminal velocity: your speed will not increase because the forces negate each other, and thus your air resistance force will not increase either.)

Something like this:

 float air_reistance_strength= r.velocity.magnitudeSquared * some_constant_like_air_viscosity;
 r.AddForce(r.velocity.normalized * -1.0f * air_reistance_strength);
Comment
Add comment · 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
2

Answer by Arycama · Sep 28, 2015 at 06:16 AM

An easy way to do this would be to get the world velocity of the object, convert it into local velocity, modify the Z component of the local velocity (Which would be the forward axis of the object), convert the modified local velocity back to world velocity, and then set that as the new velocity of the object. Something like:

 Vector3 localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
 localVelocity.z = targetSpeed;
 rigidbody.velocity = transform.TransformDirection(localVelocity);
Comment
Add comment · Show 1 · 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 happybro96 · Jul 26, 2021 at 08:46 AM 1
Share

This worked for me perfectly. I think this is the answer everyone's been looking for. Thanks man

avatar image
1

Answer by Addyarb · Sep 25, 2015 at 10:18 PM

I'm pretty sure AddForce accepts a Vector3 as a parameter, so why not try something like:

 void FixedUpdate() {
         Vector3 ForwardForce = new Vector3 (0,0,targetSpeed);
         rb.AddForce(ForwardForce);
     }
Comment
Add comment · 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 LukeNukem44 · Jan 06, 2019 at 01:10 AM 0
Share

Right...

FFS - Everyone knows how to add force in one direction!!!

The question is, HOW DO YOU DO IT WITHOUT SETTING THE OTHER AXIS TO ZERO LI$$anonymous$$E YOU SO OBTUSELY DID?!

avatar image Ady_M LukeNukem44 · Jan 06, 2019 at 02:05 AM 0
Share

@LukeNukem44 You need to calm the F down, because you obviously don't understand how AddForce works. He did NOT set the other two axes to zero. It's in the name, man... ADD... force.

avatar image
1

Answer by Ady_M · Jan 06, 2019 at 02:01 AM

@Arycama's answer should be the accepted one.

 

Here's a Quaternion version of it that does not depend on Transform's methods in case you need to work with vectors that do not directly belong to a Transform, Rigidbody, etc:

 Vector3 localVelocity = Quaternion.Inverse (transform.rotation) * rigidbody.velocity;
 localVelocity.z = 0; // Whatever forward value you want
 rigidbody.velocity = transform.rotation * localVelocity;
Comment
Add comment · 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

35 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

Related Questions

Flying Rigidbody moves forward, but doesn't turn in mid-flight? 1 Answer

Help making orbiting planets,How to rotate around the objects with the largest mass? 0 Answers

Add velocity relative to ground? 1 Answer

Use MoveRotation to Look At Another Object 0 Answers

Tracking Force Added to Kinematic Rigidbody 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