Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by MibZ · Sep 24, 2012 at 09:19 PM · physicsrigidbodyvelocityaddforcedirection

How can I convert velocity/direction to Force?

Wasn't able to find a question related to this, sorry if it's been asked a million times.

I have a GameObject that normally is controlled by Rigidbody physics that can be thrown, but before it is thrown it is moved around by code and Unity's physics are temporarily ignored. The player can drag the object around the screen, and when they stop dragging the object I want to allow physics to take over.

When the object is let go, I take the previousPosition of the object and calculate direction and velocity off of it and from here I want to convert that into Force so I can use one of the ApplyForce functions to make the object move as if it were thrown. My throw function is below, am I on the right track with this?

 private void Throw()    {            
 
         Vector3 velocity = (gameObject.transform.position - prevPos);// / Time.time;
         Vector3 direction = Vector3.Normalize(gameObject.transform.position - prevPos);
 
         gameObject.rigidbody.velocity = velocity;
 
         gameObject.rigidbody.useGravity = true; //Branch can fall now, woo! Exciting!
     }
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

3 Replies

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

Answer by aldonaletto · Sep 25, 2012 at 12:24 AM

Your script is on the right track: don't apply a force, just set rigidbody.velocity to the velocity the object had when you released it. That's physically correct: when you drag an object and release it, the object initially keeps its last velocity. You should keep track of the last position each frame, calculate the velocity and assign it to rigidbody.velocity when it's released.
I don't know how you're dragging the object, but supposing it's dragged with the mouse in a horizontal plane (perpendicular to the Y axis), the script could be like this:

Vector3 lastPos; Vector3 curVel; bool dragging = false; Plane movePlane;

void OnMouseDown(){ // object is grabbed: rigidbody.isKinematic = true; // disable physics lastPos = transform.position; // initialize lastPos dragging = true; // enter drag mode // create a logical plane at the object position and perpendicular to Vector3.up: movePlane = new Plane(Vector3.up, transform.position); }

void Update(){ if (dragging){ // create a ray from the camera passing through the mouse pointer: Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); float distance = 0f; // this will hold the distance camera->clicked point if (plane.Raycast(ray, out distance)){ // if position valid... // move the object to the plane position: transform.position = ray.GetPoint(distance); } // calculate curVel: curVel = (transform.position - lastPos)/ Time.deltaTime;
lastPos = transform.position; // update lastPos } }

void OnMouseUp(){ // object is released: rigidbody.isKinematic = false; // enable physics first! rigidbody.velocity = curVel; // copy curVel to the rigidbody dragging = false; // exit drag mode }

Comment
Add comment · Show 12 · 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 MibZ · Sep 25, 2012 at 02:56 PM 0
Share

That code is actually almost exactly what I have, just organized a little differently. The only thing I wasn't doing from that segment is using is$$anonymous$$inematic, ins$$anonymous$$d I've been disabling/enabling gravity. I'll try changing that to is$$anonymous$$inematic, thank you!

avatar image MibZ · Sep 25, 2012 at 03:29 PM 0
Share

Regardless of what size value I give to the rigidbody it only applies for a tick or two before it ends up with it's only movement being downwards because of gravity.

Is there some kind of falloff modifier I don't know about? I'm currently only modifying useGravity, is$$anonymous$$inematic, and velocity.

avatar image Fattie · Sep 25, 2012 at 03:48 PM 0
Share

AHHHHH !!!!!!!

dude, AddForce only works at that moment.

what you want to do is

(A) continuously add force, for say a few seconds .. do so in FixedUpdate or Update (ask another question about which to use, or read up!) on every frame dude!!!

that's how AddForce works, you don't just "use it once"

AND ALSO ANOTHER POSSIBILITY ..

(B) look in to the awesome CONSTANT FORCE component which is there for you

everyone here assumed you were using AddForce on an angoing basis, that's how it's used.

Just use Invoke or a simiar timer to stop your own system that applies force.

so like

 applyForceNow = true
 in update ... if ( applyForceNow) .. AddForce
 somwehere else ... Invoke("endTheForce", 2.25);
 somewhere else...
 function endTheForce(){applyForceNow = false;}

you can also profutably use Yield to apply the force over time, but start by doing it in IUpdate.

avatar image MibZ · Sep 25, 2012 at 04:07 PM 0
Share

I was originally trying to use AddForce, but I have since changed to just setting the rigidbody's velocity to my calculated velocity per @Aldo's advice.

I'll try this now, but my problem with this is that I want the object to bounce off of enemies when it hits them and if I have to modify the physics by code each frame I don't know how to do that well enough to get the effect I want. Shouldn't Unity's physics be able to take values I give it and then let physics calculations take over?

I know I could only use AddForce until it collides with something it bounces off of easily, but if I do that wouldn't it resume just falling down because of gravity and not have velocity other than being pulled down again?

avatar image Fattie · Sep 25, 2012 at 04:16 PM 0
Share
  1. there is nothing wrong with setting the velocity (in this situation). it's great

So you're done. Tick the question, move on, and never speak to me again! :)

  1. REGARDING "AddForce". First, read point (1). Now, a discussion about AddForce. An absolute basic of Unity is that you use AddForce IN EACH FRA$$anonymous$$E, it's not a one-time thing. I'm please to be the person who has told you this. All your life now you can say, man, I always remember that day Fattie on the list told me how AddForce works! Facepalm!

So now you know - enjoy.

Every single time you use Unity you will probably have to use AddForce, now you know the basic paradigm. Also do not neglect the awesome "constant force" facility in Unity.

  1. "... have to modify the physics by code each frame..." again you DEFINITELY should just set the velocity in this situation, see 1. O$$anonymous$$? but IF you were using AddForce, you wouldn't be "modifying the physics" ... that's just simply how you use AddForce. ok?

Regarding your final paragraph. I have no idea what you're talking about. It should and will all work beautifully using setting the velocity.

After the person lets go, quite simply the object WILL BEHAVE AS IN THE REAL WORLD. THat's that. End of story.

Recall that it is ESSENTIAL that in physics games all your objects have real world sizes in meters, real world masses in $$anonymous$$ilograms, and reasonably realistic "physic materials", choose those correctly for wood, metal, rubber, whatever.

IF you want some object to do something STRANGE, ie NOT what would happen in the real universe, then ask a new question about that. I'll be here for you buddy ! :)

Pls see point (1) again - it is the critical element here.

Show more comments
avatar image
0

Answer by DaveA · Sep 24, 2012 at 09:29 PM

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html right? So just use your 'direction' vector.

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 MibZ · Sep 24, 2012 at 09:41 PM 0
Share

Well that's the function I want to use, but I've tried plugging in both velocity and direction as well as different force modes but they don't seem to do anything.

If I set it to a ridiculous hard coded number for testing it moves, but it doesn't move when I use either the velocity or direction declared inside the function.

EDIT: Doesn't move a noticeable amount or in the manner that I want, I get that it's moving a teeny tiny bit.

avatar image
0

Answer by Thaina · Aug 17, 2013 at 04:44 AM

Physics : Force need mass to calculate; F = ma

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.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

AddForce vs Velocity issues with Rigidbody2D 2 Answers

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

Countering AddForce by Modifying velocity 3 Answers

Why does writing to rigidbody.velocity after AddForce stop my rigidbody moving? 4 Answers

How to decrease inertia for a rolling ball 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