Jumping?

Hi!

I’ve been using Unity for a good two months now and am happy with it. (Still haven’t gotten to the point where I can script on my own though…scripting for me usually consists of copy/paste of other scripts to compile my own for my purpouses)

However, this one, I can’t figure out. I have a sphere controlled by the accelerometer and smoothfollowed by the camera. I have a pause button in the top left and a quit button on the top right. I am trying to figure out a way to make the sphere “jump” when the screen is tapped but I still want the buttons to work (they are VERY small to avoid accidental touches)

Even if someone gives me a rough version of this, I’d be grateful.

All help is appreciated!

EDIT:
PS. the sphere is already a rigidbody

I’m assuming that you’re doing ray-casts for the buttons. If so, just do something like this (psuedo-code):

if(raycast hits button collider)
{

}
else
{
ballObject.rigidbody.AddForce(0,jumpForce,0);
}

If you’re using GUI buttons instead of raycasts, then probably the flat-out easiest way to detect a touch of the screen is to use either Input.GetMouseButton(0) (which will return true if the screen is being touched) or Input.GetMouseButtonDown(0) (which returns true if the screen was touched anew since the last frame). Though you’ll still have to figure out a way to tell if the touch was already handled by your GUI.

One quick way might be to use the screen position of your buttons and compare it to the screen coordinates of the touch. You can get the screen coordinates with Input.mousePosition.

Then just check the X and Y components of the position against the x and y extremities of your button’s bounding rectangles.

I know, I know, everyone’s probably saying, “why use mouse input routines when you have touch input available?” Well, for simple touching, it works just as well, plus, it’ll easily port straight to regular Unity if you ever decide to bring your project over for a desktop build.

Okay, thanks for that! I have converted it to this as a rough-version:

var jumpForce : int = 350;

function OnGUI () { 
   if (GUI.Button (Rect (360,280,120,40), "Jump")) { 
      rigidbody.AddForce(0,jumpForce,0);
   }
   
}

However, the user can just repeatedly press jump all they want…how can I limit it so that they can only jump every two seconds and limit the number of consecutive jumps to two? (enabling a double jump but not a triple jump, etc.)

(again, thanks a lot)

Yep, that’s another way to go.

Hmmm… well, for one thing, you probably only want to allow the user to jump when they’re on the ground. So I’d either check the Y component of the ball’s transform.position, or if your levels aren’t all the same height, then I’d cast a ray from your ball downward and then check the length to see how high off the ground your ball is. So something like this:

RaycastHit hit;
Physics.Raycast(ball.transform.position, -Vector3.up, hit, 999f, kDefaultRaycastLayers);

if(hit.distance < threshold)
   Jump();

Where “threshold” would be how close you want the ball to be to the ground before they can jump. Also, be aware that when you raycast from the ball’s position, if the ball has a collider (which I’m sure it does), then you may hit the ball’s own collider first and that’s what you’ll get instead of the distance to the ground. In which case you’ll need to offset the position enough to compensate for the radius of your ball.

If you’re wanting something more like an energy meter that restricts how many times you can jump in a space of time, then I’d just have a float that holds your “energy” value, then when you jump, set it to 0. Then, probably in FixedUpdate (since it gets called at a known rate), add a small amount back to the energy value so that it replenishes over time. Then in your jumping code, always check your energy level first, and if it is below the threshold you’ve set, then don’t jump.

I hope that helps.

Thanks a lot!

I’m a little new to this raycast thing and I’m learning so I hope to get this out of the way in the second release!

Once again, thanks a lot.

Submitted to Apple as “Rolly”…hope to have it out by X-Mas (they’re closed on the 24!!!)