K
K
Kirill Nikolaev2020-01-03 20:55:22
Unity
Kirill Nikolaev, 2020-01-03 20:55:22

How to add jump to joystick in unity?

I have 2 scripts that are linked. Here is the first one, which is mainly responsible for the flip function
using UnityEngine;
public class HeroController : MonoBehavior
{
public static Animator anim;
public static float moveInput = 0;
private bool facingRight = true;
public float speedMain = 0;
Rigidbody2D rb;
void Start()
{
anim = GetComponent();
rb = GetComponent();
moveInput = 0;
}
void FixedUpdate()
{
rb.velocity = new Vector2(moveInput * speedMain, rb.velocity.y);
if (moveInput > 0 || moveInput < 0)
{
anim.SetInteger("Anim", 2);
}
else
{
anim.SetBool("run", false);
}
if (facingRight == false && moveInput > 0)
{
gameObject.GetComponent().flipX = false;
Flip();
}
else if (facingRight == true && moveInput < 0)
{
gameObject.GetComponent().flipX = true;
Flip();
}
}
public void Flip()
{
facingRight = !facingRight;
}
}
And here is the second one, which is already responsible for the movement through the joystick:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cherocntrlbtn : MonoBehavior
{
public Joystick joystick;
Rigidbody2D rb;
Vector3 position;
private void Update()
{
if (joystick.Horizontal > 0.1f)
{
HeroController.moveInput = 1;
}
else if (joystick.Horizontal < -0.1f)
{
HeroController.moveInput = -1;
}
else
{
HeroController.moveInput = 0;
}
}
} Here.
And for some reason I can not add my code so that the Persian jumps if the joystick is moved up. Please help.
If suddenly you also wrote a game with a joystick, please send your script.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question