Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
public class JoystickMobile : MonoBehaviour
{
public Transform player;
public float speed = 5.0f;
private bool touchStart = false;
private Vector2 pointA;
private Vector2 pointB;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Camera.main.transform.position.z));
}
if (Input.GetMouseButton(0))
{
touchStart = true;
pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}
else
{
touchStart = false;
}
}
private void FixedUpdate()
{
if(touchStart)
{
Vector2 offset = pointB - pointA;
Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
moveCharacter(direction*-1);
}
}
void moveCharacter(Vector2 direction)
{
player.Translate(direction * speed * Time.deltaTime);
}
}