- Home /
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!
}
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 }
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!
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.
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.
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?
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! :)
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.
"... 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.
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.
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.