- Home /
Regarding Jumps, Heights, and Scripting
Hello. After a long time of no activity, I have forgotten a lot about programming. Now. I have a 2D sprite character that the player will control. This character has a simple 2D box collider and a rigidbody. I have figured out how to make the little guy move use vectors, and have gotten him to go up. What I want to know is how to make him stop going up after reaching a specific height (say, 2 meters above where he started), and come back down. Here is the script I have thus far. A little help in collision detecting would be useful as well. Thanks in advance.
public var speed : int = 5;
public var jumpSpeed : int = 10;
public var gravity : int = 10;
function Update () {
rigidbody2D.AddForce (-Vector2.up * gravity);
if (Input.GetAxis("Horizontal") > 0)
transform.Translate (Vector2.right * speed * Time.deltaTime);
if (Input.GetAxis("Horizontal") < 0)
transform.Translate (-Vector2.right * speed * Time.deltaTime);
if (Input.GetButton("Jump"))
rigidbody2D.velocity = Vector2.up * jumpSpeed;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613130500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Help With Simple Jump Script 1 Answer
Problems with jumping script 1 Answer
Is there any ifNot.Grounded command? 1 Answer