Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jump by touch Unity C#

I writing game on Unity C#

This is simple runner.

I have Platformer2DUserControl Script.

Here is it

 using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;

namespace UnitySampleAssets._2D

{

[RequireComponent(typeof (PlatformerCharacter2D))]
public class Platformer2DUserControl : MonoBehaviour
{
    private PlatformerCharacter2D character;
    private bool jump ;

    private void Awake()
    {
        character = GetComponent<PlatformerCharacter2D>();
    }

    private void Update()
    {
        if (Input.touchCount > 0)
            character.Move(1, false, jump);

        if (!jump)
        // Read the jump input in Update so button presses aren't missed.
        jump = CrossPlatformInputManager.GetButtonDown("Jump");
    }

    private void FixedUpdate()
    {
        // Read the inputs.
        bool crouch = Input.GetKey(KeyCode.LeftControl);
       // float h = CrossPlatformInputManager.GetAxis("Horizontal");
        // Pass all parameters to the character control script.
        character.Move(1, false, jump);
        jump = false;
    }
}
}

I try to make that player jumps when i touch screen on my phone.

But it don't jump.

What wrong in my code?

Thank's for help.


1 Answers

If you just want the the object to jump on touch use

Input.GetButton("Fire1")

It is predefined to left mouse click and also work for single touch input

like image 111
trahane Avatar answered Feb 15 '26 14:02

trahane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!