- Home /
Make object continue to move up as long as button is pressed
Hi,
Sorry for the noob questions but I have a bird in a third person view. And when I press and hold W I want the bird to fly up continuously until I release W at which point the bird should glide down (bird has gravity).
I am having a bit of a hard time making the bird go up continuously. Everything else works fine.
I have tried using transform.Translate, rigidbody.AddForce, and CharacterController but my solutions are not working as I want them to.
With how it is right now, when I press and hold W the bird moves up but stops going up after a certain point and begins to fall down even if I am still holding the W key. And when it is falling, if I press W it does not go up again, it will fall to the ground, and only when it is on the ground I can make it go up again. Which seems quite strange to me...
Anyone have any suggesions?
1.
if (Input.GetKey(KeyCode.W))
{
anim.SetBool("isFlying", true);
transform.Translate(Vector3.up * Time.deltaTime, Space.World);
}
2.
if (Input.GetKey(KeyCode.W))
{
anim.SetBool("isFlying", true);
transform.Translate(new Vector3(0, speed * Time.deltaTime, 0));
}
3.
if (Input.GetKey(KeyCode.W))
{
anim.SetBool("isFlying", true);
rigidBody.AddForce(new Vector3(0f, flyUpForce, 0));
}
Answer by bennett_apps · Oct 08, 2018 at 11:10 PM
try Debug.Log...is your code actually executing?
I think it is executing because the animation plays just fine and it does go up, But the problem is that it does not continuously go up, after a certain height it just drops on its own. And when it is dropping, even if I press W again it just continues to fall.
that happens with all three examples? Same result?
Answer by JoelGab · Oct 09, 2018 at 12:55 AM
My guess is code somewhere else in your project is resulting in the odd behaviour. The fact you said it stops at a certain point makes me believe some code, somewhere else is doing that.
Right, so in theory the above codes should work to get the effects I am describing?
Yes, Assu$$anonymous$$g no other code is applying a downward force exponentially, which might be why you inevitably start to sink down. If you coded your own gravity maybe look into the logic there.
Answer by JusSumGuy · Oct 10, 2018 at 05:31 AM
Are all these if statements in the Update function? if so that's the problem.
To clarify, if the if statements are not in the Update function that's the problem. lol
@JusSumGuy That wouldn't make sense, because it wouldn't be triggering at all if it was not Update.