🕹️ Créer un Joystick Virtuel avec UNITY – Déplacement pour périphérique mobile.

Dans ce tutoriel vous apprendrez à créer de toute pièce un Joystick Virtuel pour vos projet à destination des plateformes mobiles.

Voici les sprites Utilisés dans ce tutoriel :

Code source du projet :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Joystick : MonoBehaviour
{
Vector2 startPoint, endPoint, InitialPoint;
bool touchStart = false;
public float speed = 5f;
public Transform Player;
private void Start()
{
InitialPoint = transform.position;
}
private void OnMouseDown()
{
startPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private void OnMouseDrag()
{
touchStart = true;
endPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private void OnMouseUp()
{
touchStart = false;
}
private void Update()
{
if (touchStart)
{
Vector2 offset = endPoint - startPoint;
Vector2 direction = Vector2.ClampMagnitude(offset, 1f);
movePlayer(direction);
transform.position = new Vector2(startPoint.x + direction.x, startPoint.y + direction.y) ;
}
else
{
transform.position = InitialPoint;
}
}
void movePlayer(Vector2 direction)
{
Player.Translate(direction * speed * Time.deltaTime);
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Joystick : MonoBehaviour { Vector2 startPoint, endPoint, InitialPoint; bool touchStart = false; public float speed = 5f; public Transform Player; private void Start() { InitialPoint = transform.position; } private void OnMouseDown() { startPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); } private void OnMouseDrag() { touchStart = true; endPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); } private void OnMouseUp() { touchStart = false; } private void Update() { if (touchStart) { Vector2 offset = endPoint - startPoint; Vector2 direction = Vector2.ClampMagnitude(offset, 1f); movePlayer(direction); transform.position = new Vector2(startPoint.x + direction.x, startPoint.y + direction.y) ; } else { transform.position = InitialPoint; } } void movePlayer(Vector2 direction) { Player.Translate(direction * speed * Time.deltaTime); } }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Joystick : MonoBehaviour
{
    Vector2 startPoint, endPoint, InitialPoint;
    bool touchStart = false;

    public float speed = 5f;
    public Transform Player;

    private void Start()
    {
        InitialPoint = transform.position;
    }
    private void OnMouseDown()
    {
        startPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    private void OnMouseDrag()
    {
        touchStart = true;
        endPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }
    private void OnMouseUp()
    {
        touchStart = false;
    }

    private void Update()
    {
        if (touchStart)
        {
            Vector2 offset = endPoint - startPoint;
            Vector2 direction = Vector2.ClampMagnitude(offset, 1f);
            movePlayer(direction);
            transform.position = new Vector2(startPoint.x + direction.x, startPoint.y + direction.y) ;
        }
        else
        {
            transform.position = InitialPoint;
        }

    }
    void movePlayer(Vector2 direction)
    {
        Player.Translate(direction * speed * Time.deltaTime);
    }
}

Turotiel Vidéo :

YouTube player

A propos de upln 280 Articles
En informatique le problème se situe souvent entre la chaise et le clavier !

Soyez le premier Ă  commenter

Poster un Commentaire

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.