- Home /
Control speed an rigidbody
I'm using this simple script when I hold the right to the speed increases smoothly but I do not know, do the speed back to zero smoothly see my script:
public float acelerate = 5.0f;
public float speedMin = 10.0f;
public float speedMax = 30.0f;
private float moveZ;
public float speed = 0.0F;
private float V;
private float H;
public float timePassed;
void FixedUpdate () {
V = Input.GetAxis("Vertical") *Time.deltaTime;
H = Input.GetAxis("Horizontal")*Time.deltaTime;
Vector3 vt = new Vector3(-V,0, H).normalized;
rigidbody.AddForce( 0, 0, vt.z*speed*8*Time.deltaTime);
if(H>0){
timePassed = Time.time;
speed = Mathf.Lerp(speedMin, speedMax, timePassed/acelerate);
}else{
// my problem....... help me back for zero smoothly?
}
Why do you not just reverse your logic for lerping your force "forward"?
Answer by robertbu · Oct 24, 2013 at 02:22 AM
You are mixing and matching things here. Think about a rocket in space. If you keep adding force (since there is no drag), the rocket will go faster and faster. Your code, and specifically your calculation of speed is does not work as any sort of real world calculation. For example using Time.time in the calculation assures that the longer the game runs, the more force you will add per frame. In addition, it seems strange to use horizontal twice...once in calculating vt, and once again to make speed calculations. My suggestion.
Delete lines 20 - 26 and 'speed' altogether.
Create a forceFactor variable and initialize it to a non-zero value. Start with 5.0;
In the inspector, select the object this script is attached to and increase the 'Drag' setting in the Rigidbody component (it is zero by default).
Your AddForce() will look something like:
rigidbody.AddForce(0,0, H*forceFactor);
Since you are executing this in FixedUpdate(), you don't need deltaTime or fixedDeltaTime. Your code will all be about balancing your forward force and your drag. The drag will slow your ship down when you back off of 'Horizontal'. Play with some values until you get the ship to behave like you want.
good let tell you, i did this way why i don't your $$anonymous$$d!!!
void control_speed(){
R1 = Input.Get$$anonymous$$ey($$anonymous$$eyCode.JoystickButton7);
// run
// obs: increase speed if you are not running
if(R1 && speed <100){
speed +=50.0f*Time.deltaTime *100* Time.deltaTime;
// stop run
//decrease speed if are not run
}if(speed >40 && !R1 && H >0
|| speed >40 && !R1 && H <0
|| speed >40 && !R1 && -V >0
|| speed >40 && !R1 && -V <0){
speed -=50.0f *Time.deltaTime*100* Time.deltaTime;
}
// walk
// obs: increase speed if walking
if(speed <40 && !R1 && H > 0
|| speed <40 && !R1 && H < 0
|| speed <40 && !R1 && -V < 0
|| speed <40 && !R1 && -V > 0){
speed +=50.0f*Time.deltaTime *100* Time.deltaTime;
}
// stop walk
// obs: decrease speed if not walking
if(speed >0.05 && !R1 && H ==0 && V ==0){
speed -=50.0f *Time.deltaTime*100* Time.deltaTime;
}
}
good let tell you, i did this way why i don't your $$anonymous$$d!!!
I don't understand what this means.
Given your logic here, there are a couple of things for you to consider. First, if you want to use explicit speeds with a Rigidbody, then you want to directly assign Rigidbody.velocity, not use AddForce(). Second, for checking speed with a rigidbody, you can use Rigidbody.velocity.magnitude. Or since all of your speed is in the 'Z' direction, you can check Rigidbody.velocity.z. It seems really convoluted to me to calculate some arbitrary 'speed' variable.
FYI: If you just want a fix for your current logic and want to slow down, a simple way is to multiply the velocity by some value less than 0 in FixedUpdate(). Put the following line in your else clause where you have "my problem....... help me back for zero smoothly?"
transform.velocity *= 0.98;
Adjust the 0.98 as appropriate for your app. This does not mimic real physics, but it looks good for many applications.
the logic i posted serve to when i hold the button in control to right, then the value var "speed" increases, and when i drop it then o value "speed" it will decrease.
its just it i want do increase the speed of the player smoothly and when i drop it the button the value will decrease you understand me?
Here is a simple script that creates a linear increase in speed. Turn off gravity, and set Drag to 1.0 in the Rigidbody component.
using UnityEngine;
using System.Collections;
public class Bug25c : $$anonymous$$onoBehaviour {
private float speedIncreasePerFrame = 0.1f;
void FixedUpdate() {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow)) {
float speed = rigidbody.velocity.magnitude;
rigidbody.velocity = transform.forward * (speed + speedIncreasePerFrame);
}
}
}
The 'Drag' setting in the Inspector controls how quickly it slows down. The 'speedIncreasePerFrame' controls how quickly it speeds up. But note this is a linear increase. You may want to use a percentage ins$$anonymous$$d.
Note you could do this with AddForce() ins$$anonymous$$d. Add a bit of force each frame to speed up. Let 'Drag' slow it down.
tell me what diferrence rigidbody.velocity and rigidbody.addForce? i think you dont understand me i want control o accelerate the variable "speed" to increase and decrease
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Help me with smooth zoom 1 Answer
Time Freezer 1 Answer
Smooth Camera 1 Answer
Animation speed control - no smooth animation on slow down 3 Answers