NEW INPUT System in Unity - Mobile Input Tutorial

preview_player
Показать описание
In this video, you will learn how to implement the new input system in Unity, as well as the mobile controls in your Unity project. The reason why you should use the New Input System is because the old Input System will be deprecated in future versions. The input system covered in this video is very practical and you can implement it easily.

The first thing you need to do is to open Package Manager and install the Input System. Then you should make a system of Input Actions that will be powering your project with some specific controls. Next, you have to go to the PlayerMovement script and implement the InputSystem into the void Awake, which is called before void Start and Update.

The script used in this video describes what exactly is required for making the New Input System work properly. It's a very simple Unity tutorial that can be useful for beginners.

I was making a break from Unity and it is time to start making new tutorials. This tutorial is the fourth video in the 2D platformer tutorial series.

Make sure to add the Canvas element because it contains all important layouts and assets that allow you to add the UI elements in your project. The UI elements need to have an OnScreen Button action that is defined with arrow keys (movement) or spacebar (jumping). These buttons are for navigation on mobile devices. I will also make a video with Android remote display that will easily demonstrate the input system feature.

If you find this video helpful, consider subscribing. I will make videos every week. Lastly, I will make more game development tutorials and other content related to game industry. These videos will help beginner game developers become better and make amazing games.

Support the Creator of Kozmobot on the social networks!

Music:
City Walk - John Patitucci
Lyric Melody for Solo Bass - John Patitucci
Рекомендации по теме
Комментарии
Автор

Man! you have no idea how desperately I was searching for this... Thank you!!!

avanishpatitripathi
Автор

Great video. Too, I really like the jazz bass; It's refreshing to hear and provides the entire video with a cool, fun vibe.

A_Name_
Автор

InputSystem does not contain definition for land

AliShàaban-mx
Автор

When I tried this message came
ScriptableSingleton already exists. Did you query the singleton in a constructor?
Please help

ElogeAdjra
Автор

my variable "direction" not set the float value in direction, you can fix it?

rodrigooracio
Автор

Very helpful tutorial! Just one little problem, the Land is not working and it says input definition does not contain a definition for it!

sayhan
Автор

the jump work but move right and left doesn't work can you fix it and
(I made the bug fix that is in your video and it still doesn't work)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
PlayerControls controls;
public bool isFacingRight = true;
private Rigidbody2D rb;
private BoxCollider2D coll;
private SpriteRenderer sprite;
private Animator anim;
float direction = 0;

[SerializeField] private LayerMask jumpableGround;

private float dirX = 0f;
[SerializeField] private float moveSpeed = 7f;
[SerializeField] private float jumpForce = 14f;

private enum MovementState { idle, running, jumping, falling }

[SerializeField] private AudioSource jumpSoundEffect;

void Awake()
{
controls = new PlayerControls();
controls.Enable();

+= ctx =>
{
direction = ctx.ReadValue<float>();
};

controls.Land.Jump.performed += ctx => Jump();
}

void Jump()
{
if(IsGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}

// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody2D>();
coll =
sprite =
anim =GetComponent<Animator>();
}

// Update is called once per frame
private void Update()
{
dirX =
rb.velocity =new Vector2(dirX * moveSpeed, rb.velocity.y);

if (Input.GetButtonDown("Jump") && IsGrounded())
{
jumpSoundEffect.Play();
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}

updateAnimationState();
}

private void updateAnimationState()
{
MovementState state;

if (dirX > 0f)
{
state = MovementState.running;
sprite.flipX = false;
}
else if (dirX < 0f)
{
state = MovementState.running;
sprite.flipX = true;
}
else
{
state = MovementState.idle;
}

if (rb.velocity.y > .1f)
{
state = MovementState.jumping;
}
else if (rb.velocity.y < -.1f)
{
state = MovementState.falling;
}

anim.SetInteger("state", (int)state);
}

private bool IsGrounded()
{
return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}

void FixedUpdate()
{
if (isFacingRight && direction < 0 || !isFacingRight && direction >0 )
Flip();
}
void Flip()
{
isFacingRight = !isFacingRight;
transform.localScale = new Vector2(transform.localScale.x, transform.localScale.y);
}
}

Abdelrhman
Автор

where can I find the on-screen button script if i didn't make it?

GDK-
Автор

is said InputSystem could not be found.

Blankk__mind
Автор

I think there's no need to multiply with time.deltatime if you use unity's rigidbody pretty sure they handle the frame independency themselves so you just fk it up if you multiply 2 times.

davidalin
Автор

Im not using any C# generated class
i tried but i it gives error
Im struggling right now coz my UI buttons stay pressed
so the character keeps moving in one direction
this problem doesnt occur on pc
any reason why this is happening?

SwagMolten
Автор

thanks man but player in not flip plz help me brooo plz

omborse-pwfl