Hello, I’m making a simple game character that is controlled by a virtual joystick on the UI. The Joystick and character movement scripts are working, but the problem I’m having is to animate the character movement.
I Made the character model, movement and idle animations on Blender, I tested them on another scene with another movement script that uses the ASDW inputs to move, but what I really want to do is to make the character move and be animated with the virtual joystick input.
Can anyone help me? Also, if possible, does anyone knows how to change the animation direction accordingly to the movement direction?
In this link there’s two images of what I got: http://imgur.com/a/zyIfo
And here’s the Virtual Joystick and Movement Scripts:
''Virtual Joystick ‘’ (Goes on Joystick Image in the UI)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Player : MonoBehaviour
{
public float moveSpeed = 5f;
public VirtualJoystick joystick;
public Rigidbody rb;
public void Start()
{
rb = GetComponent ();
}
private void Update ()
{
rb.transform.Translate (joystick.inputVector.x * moveSpeed * Time.deltaTime, 0, joystick.inputVector.z * moveSpeed * Time.deltaTime);
}
}
‘‘Movement’’ (Goes on Character)
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
public Vector3 inputVector;
private void Start()
{
bgImg = GetComponent();
joystickImg = transform.GetChild(0).GetComponent();
}
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 / 2)
, inputVector.z * (bgImg.rectTransform.sizeDelta.y / 2));
Debug.Log(pos);
}
}
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector3.zero;
joystickImg.rectTransform.anchoredPosition = Vector3.zero;
}
public float Horizontal()
{
if (inputVector.x != 0)
return inputVector.x;
else
return Input.GetAxis(“Horizontal”);
}
public float Vertical()
{
if (inputVector.z != 0)
return inputVector.z;
else
return Input.GetAxis(“Vertical”);
}
}
I hope that someone can help, thanks