- Home /
Rolling ball with rigidbody
I'm trying to make simple ball game where player is supposed to roll a ball. I get ball rolling, that's not a problem but I'm unable to make it "speed up".
My current code looks like this:
void update() {
force = Vector3(Input.GetAxis("Horizontal") * maxspeed * Time.deltaTime, 0, Input.GetAxis("Vertical") * maxspeed * Time.deltaTime);
}
void fixedUpdate() {
rigidbody.addTorque(force);
}
This works so that ball accelerates quite quickly to maxspeed and stays there. But I would like to speed up more when axis button is kept pressed for longer times. How I can achieve that?
Time.deltaTime inside of Update is different from the Time.deltaTime in FixedUpdate- if you create the value in one, it'll be wrong in the other!
It still records the time the key is pressed, so it doesn't matter. 1 second in update is the same as 1 second in fixedupdate. I advice to use it in Update() because that function is called more often so results would be more accurate.
Answer by arcghost · Jan 10, 2012 at 07:34 PM
You will have to record the time the keys are pressed. You can use an if-statement to check if a key/axis is pressed and Time.deltaTime to check how much time passed since the frame is complete.
void Update () {
if( Input.GetAxis(" --the axis you want to check-- ") != 0 ){
timePressed += Time.deltaTime; //make a new var to keep track of the time.
increaseMultiplier();
}
else{
timePressed = 0; //The button is not being pressed anymore.
decreaseMultiplier();
}
}
You will have to make a multiplier variable to apply to the speed of the object and adjust that value. I hope this helps.
Thank you, this got me on right track. I had to change code a bit from using torque to applying force due limitation in angular velocity. I tried to set higher angular velocity limit but it creates unwanted rotation.
Answer by nowarelabs · Jan 11, 2012 at 02:55 PM
Apparently rigidbody has defined maximum angular velocity (7 by default). I can change it but then feeling of rotation is something that I do not want.
Maybe I should use force (not torque) and do rotating ball using transform?
Your answer
Follow this Question
Related Questions
Guided Missile 0 Answers
Physics object slides while turning. 1 Answer
Problem with joystick movement 1 Answer
Addtorque towards a point, compensate for its inertia? 0 Answers
Adding torque on an object's axis 2 Answers