- Home /
I can't get a joystick to move my player
So I've tried everything I could find, and nothing works. Can some please tell me how I would make my player move with this code and what I have to add. The player needs to be able to move in every direction like in Last day on earth survival and doesn't have to jump. I'm still learning C# so sorry if I'm missing something simple. I followed the N3K EN joystick tutorial and it's working but I don't know how to make my player move.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class JoyStick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler {
private Image joystickBG;
private Image joystickS;
public Vector2 pos;
public static Vector3 inputVector;
private void Start()
{
joystickBG = GetComponent<Image>();
joystickS = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnDrag(PointerEventData ped)
{
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(joystickBG.rectTransform
, ped.position
, ped.pressEventCamera
, out pos)) ;
{
pos.x = (pos.x / joystickBG.rectTransform.sizeDelta.x);
pos.y = (pos.y / joystickBG.rectTransform.sizeDelta.y);
Debug.Log(pos);
inputVector = new Vector3(pos.x * 2, 0, pos.y * 2);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
joystickS.rectTransform.anchoredPosition =
new Vector3(inputVector.x * (joystickBG.rectTransform.sizeDelta.x / 3)
, inputVector.z * (joystickBG.rectTransform.sizeDelta.y / 3));
}
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector3.zero;
joystickS.rectTransform.anchoredPosition = Vector3.zero;
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
}
Answer by AdamOliver · Jun 11, 2018 at 07:01 PM
Unless I'm missing something obvious this script will only make the joystick image move, there's nothing in there to control a player. Around line 13 add public Transform player;
, then at the bottom of your OnDrag method, you can use player.translate(inputVector)
to move your player's transform by the input vector. Then in the inspector don't forget to drag your player into the "player" field on this script.
Thank you, it worked! but how do I make it set a bool to activate animation and how can I make it go a little bit slower? If I want it to play the animation do I do this
public void Update()
{
if (inputVector.magnitude != 0)
{
player.Translate(inputVector);
Animator.SetBool("Walk", true);
}
if (inputVector.magnitude = 0)
{
Animator.SetBool("walk", false);
}
}
I'm afraid I haven't worked with animators so I'm not sure on that one, if you still aren't sure maybe ask it as a new question
Your answer
Follow this Question
Related Questions
Character triple+ Jumping: Please Help! 0 Answers
Slime script? 1 Answer
Having problem with detecting movement 1 Answer
How to make a square mesh and remove a part of it in real time? 0 Answers
AI Movement Direction 0 Answers