android, roll a ball, using single touchpad to jump

Hi, I am having a go at creating a small game. I am using the roll a ball prefab in a small boxed area with a couple of ramps and platforms.

I have this working fine when i tilt the device the ball rolls around perfect.

what I want to know is: I have added the single touchpad prefab (from standard mobile assets) to the right hand side of my screen, when I build the game and press it I can see that it is recognising the touch.

Is it possible to add a script to the touchpad or to my roll a ball prefab to make the ball jump in the air such as:

rigidbody.AddForce (Vector3.up * 10);

when the touchpad is touched? without messing up the roll a ball controls?

any help, suggestions, clues appreciated! :slight_smile:

Well, the moment you touch you should indeed do: rigidbody.AddForce(Vector3.up * 10, ForceMode.Impulse);

The ForceMode.Impulse is something I always use for jumping.
I don’t exactly know what you’re trying to achieve, do you want to change the existing script of the touchpad, or do you want to write your own script?

I will basically give you an example of using the simple MouseDown function in a seperate script.
You will need to create a new script, for example JumpOnTouch.

In this script, this is what the update should look like:

void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            rigidbody.AddForce(Vector3.up * 10, ForceMode.Impulse);
        }
    }

This script should be added to your ball, which has the rigidbody attached.
With this code your ball will jump when you press the screen.
I would also change the parameter Vector3.up * 10 to Vector3.up * force, where force can be a public variable to adjust the force of your jump.

If you want to limit the position where you should click to jump, you can just use the Input.mousePosition to write some extra checks for where the player clicks.

You should perhaps also check if your player is grounded in order to jump. (check raycasting)

I hope this helps you on your way.

Cheers :wink: