- Home /
C # Applying Velocity to Rigidbody
Hey all, I've got a bit of an issue here. I'm trying to add velocity to a gameObject so that after it has been moved it will just slowly come to a stop. I have to do it in C# and C# is a language that I dont have much experience with.
Below is the sample code that I'm using. Essentially, when the code inside "Input.GetMouseButtonUp(0)" is executed, I want velocity to be applied to the gameObject.rigidbody that has been moved.
private Vector3 movement;
protected GameObject pivot;
public override void handleSingleTouch(iPhoneTouch touch)
{
if (!allowDrag) return;
movement = touchMovementVector(touch);
this.startPivot(gameObject.transform.position);
pivot.transform.Translate(movement,Space.World);
if (Input.GetMouseButtonUp(0)) {
gameObject.rigidbody.velocity = new Vector3(0, 10, 0);
}
this.endPivot();
}
As it is, its moving the gameObject 10 for testing sake. Apologies if its something basic that I'm doing wrong, and I would appreciate any help. I have seen another question that is loosely related to my issue, but unfortunately it didnt help me much.
what do you actually want it to do? And what is it doing, I'm a bit hazy on the issue.
Hey unglyant, what I was trying to do at the time was to apply momentum to a rigidbody that had been manipulated/moved by the cursor. At the time I didnt know that the script 'DragRigidbody.js' was there to do exactly what I wanted to do, so I was barking up the wrong tree completely :s I must get a beginners guide to unity book so I can avoid asking silly questions like that again. Thanks for replying to my question too, I appreciate your help :)
Answer by DoubleDouble · Aug 11, 2011 at 10:29 PM
I think you may be wanting to use a different function than velocity to slow something over time.
The script reference for velocity is here
If you read the description, it is more suited to an instant change of direction, while using something like AddForce or AddRelativeForce would be for repeatedly "pushing" something.
You may also want to take a look at SmoothDamp, it smoothly translates a number from the current value to a target value (you could set as 0?).
You can use the script reference for Unity for looking up a lot of things. If you find the Rigidbody section it pretty much tells you all the functions it uses and you can have it show you how in C# or java.
Hi DoubleDouble, thanks for helping me out on this. Righto, at least now I know that I was looking to use the wrong function!
I'll try that out tomorrow (gotta sleep) but I took a quick look and it looks promising enough, so hopefully I'll be able do what I want to do with it :)
Yeah, I've been reading the script reference quite a lot since I've ran into this problem in the last week, but I just cant seem to figure it out :S Thanks again :)
No problem, next time click "add new comment" below my answer ins$$anonymous$$d of adding a new answer. If my answer helps you figure it out tomorrow make sure to accept it.
Hi, so SmoothDamp works alright, but turns out it doesnt entirely solve my issue but does go half the way.
I need to be able to set the target position of my rigidbody dynamically (not sure if dynamically is the word, more used of html coding :o ) ie. if the rigidbody can be moved in lots of different directions it should come to rest (the target position) in lots of different places but so far I can only set one place, the Transform target in the Unity GUI. Any ideas on how to solve this issue?
Your answer
Follow this Question
Related Questions
Moving a player with Rigidbody 2 Answers
Why doesn't my rigidbody velocity limiter script work properly? 1 Answer
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
How to make gameobjects fall down faster with each update ? 1 Answer
GameObject disappears when velocity is set to (near) zero. 1 Answer