- Home /
Joystick problems Unity 3D Mobile
Hello, I'm making slither.io mobile in unity 3d and my joystick controls don't work. The snake only moves left and right. I want it to follow the direction the joystick is pointing. Help me please. Please write very clearly as I am new to this. And sorry for my bad english. I hope, you understood me
My script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class SnakeMovement : MonoBehaviour { public float Speed; public float RotationSpeed; public List tailObjects = new List(); public float z_offset = 0.5f; public GameObject TailPrefab; public Text ScoreText; public int score = 0; public Joystick joystick; void Start() { tailObjects.Add(gameObject); }
void Update()
{
ScoreText.text = score.ToString();
transform.Translate(Vector3.forward*Speed*Time.deltaTime);
Vector3 direction1 = Vector3.up*joystick.Horizontal;
transform.Rotate(direction1*RotationSpeed*Time.deltaTime);
}
public void AddTail()
{
score++;
Vector3 newTailPos = tailObjects[tailObjects.Count-1].transform.position;
newTailPos.z -= z_offset;
tailObjects.Add(GameObject.Instantiate(TailPrefab,newTailPos,Quaternion.identity) as GameObject);
}
}
Answer by Dailyalex · Jan 13 at 08:18 AM
You should do like this:
Vector3 direction = new Vector3(joystick.Horizontal, 0, joystick.Vertical);
(but it depends in which 2 coordinates your game running, in my example it is X and Z.
And after just multiply direction by speed and do other necessary things.
Your answer

Follow this Question
Related Questions
Joystick Angle Problems 0 Answers
Change the position of the Joystick by tap 2 Answers
Xbox controller joystick axis small movements? 1 Answer
How to implement joystick? 0 Answers