Mon problème se compose de 3 problèmes similaires et m’énerve, j'ai créé un script [Solo] d'un joystick qui va me permettre de déplacer le personnage, dans la direction ou le joystick ira.
/!\Mais/!\ (il y a toujours des mais) maintenant j'aimerais faire en sorte que quand le bouton ira en bas cela activera une animation particulière que ma créatrice graphisme/animation m'aura faite mais j'y arrive uniquement avec la marche vers l'avant et le problème c'est que je trouve pas de solution pour faire l'animation sans que le script ne Bug.
J’espère que vous pourrez m'aider cela m’enlèverait une grosse épine du pied : p
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class JoystickVirtuel : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;
public GameObject player;
private Animator anim;
public void Start()
{
anim = player.GetComponent<Animator>();
bgImg = GetComponent<Image>();
joystickImg = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(bgImg.rectTransform, ped.position, ped.pressEventCamera, out pos))
{
pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x);
pos.y = (pos.y / bgImg.rectTransform.sizeDelta.y);
inputVector = new Vector3(pos.x * 2 + 1, 0, pos.y * 2 - 1);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
joystickImg.rectTransform.anchoredPosition = new Vector3(inputVector.x * (bgImg.rectTransform.sizeDelta.x / 3), inputVector.z * (bgImg.rectTransform.sizeDelta.y / 3));
anim.SetBool("Marcher", true);
Debug.Log(inputVector);
}
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector3.zero;
joystickImg.rectTransform.anchoredPosition = Vector3.zero;
anim.SetBool("Marcher", false);
}
public float Horizontal()
{
if (inputVector.x != 0)
return inputVector.x;
else
return Input.GetAxis("Horizontal");
}
public float Vertical()
{
if (inputVector.x != 0)
return inputVector.z;
else
return Input.GetAxis("Vertical");
}
}