- Home /
Smooth movement help?
Hello, I have a helicopter script and I want the the helicopter to smoothly speed up while a key is pressed, and right after JUST that key is let up, I want it to smoothly stop moving in that direction. Again, I want to be able to have each key's action independently and smoothly speed up and stop movement in that certain direction.
Here's a sample of what I have so far. Right now movement is rare and completely unpredictable/glitchy.
(In Update function)
// up
if (Input.GetKey("up"))
{
Apache.rigidbody.constantForce.force.y = 300;
upPressed = false;
}
if (Input.GetKeyUp("up"))
upPressed = true;
//down
if (Input.GetKey("down"))
{
Apache.rigidbody.constantForce.force.y = -300;
downPressed = false;
}
if (Input.GetKeyUp("down"))
downPressed = true;
//Movement realism: this smooths out all movements and rotations.
if ((upPressed || downPressed) && Apache.rigidbody.velocity.y > 0 )
Apache.rigidbody.constantForce.force.y = -100;
if ((upPressed || downPressed) && Apache.rigidbody.velocity.y < 0)
Apache.rigidbody.constantForce.force.y = 100;
else if ((upPressed || downPressed) && Apache.rigidbody.velocity.y == 0)
Apache.rigidbody.constantForce.force.y = 0;
Answer by donhelms · Aug 16, 2012 at 09:11 PM
Have you tried using
rigidbody.AddForce(yourforce,ForceMode.Impulse);
Also, what is the mass of the body?
Is your rigid body to proper scale in meters with regards to an actual helicopter?
rigidbody.addforce creates an immediate motion change doesn't it? i want constantforce because it was (vaguely) recommended by unity for that kind of movement. I haven't really given much thought to the mass or scale. Right now the scale is 500 because It's easier to navigate the view of the helicopter when it's big. The mass is 50.
Well, thinking of the propeller of a chopper, it's actually creating 'pulses' of downward force much in the way that an impulse force would work. It's the accumulation of these 'impulses' from the choppers blades that elevate the aircraft so if you model your bodies correctly in proportion of scale/weight, it may work.
The physical properties of the rigid body can potentially be the source of the erratic behavior you see. If I were you, i'd start by making sure your helicopter rigid body is the same size/scale of a real helicopter.. if a Apache is 50 meters long, then make sure your model/rigid body is 50.
Remember that the physics engine is coded to real world physics so it assumes that your scales/weights are correct as well.
So it can compute lift if the blades are correctly shaped?? Wow! Well, there's only so much I can do with unity and scripting, I'm only 17, haha. Currently, for my rotors, I'm using a hingejoint motor and adding force to it. What I'm concerned with is the input of my code and having the output be dependable. Right now, for example, holding the Up key works, but after you use that key, if you press Down, it won't move. I just want to optimize my code so that that stops happening.
Your answer
Follow this Question
Related Questions
How to Move a Cube 3 Answers
Stopping an object immediately 1 Answer
Speed pickup 1 Answer
How to incorporate Time.deltaTime into existing script 1 Answer
Change a float smooth 2 Answers