Question by
felixwcf85 · Apr 03, 2016 at 10:35 AM ·
gameobjectaddforceheightmaximum
GameObject falls instead flying horizontally at max height
Seems like a dumb question, but I would like to make my game object falls as soon as it reaches the max height, instead flying "horizontally" at max height, when the force is applied.
This is my codes:
FixedUpdate() {
if (transform.position.y >= maxHeight) {
transform.position = new Vector2 (transform.position.x, maxHeight);
spirit.GetComponent<Rigidbody2D> ().AddForce (new Vector2(0, -60));
}
}
The force is seemed "never" applied to it.
If I removed
transform.position = new Vector2 (transform.position.x, spirit.maxHeight);
It would fly over the maximum height.
How to make the game object falls immediately when it reaches the maximum height?
Comment
Answer by Masterio · Apr 03, 2016 at 10:51 AM
you should use the rigidbody.velocity vector. If max height then
spirit.rigidbody.velocity = spirit.rigidbody.velocity * -1; // or apply some const fall velocity
or reset the velocity before you add the force like:
spirit.rigidbody.velocity = Vector3.zero;
spirit.rigidbody.velocity.AddForce(new Vector3(0, -60, 0));