- Home /
Android C# JoyStick Movement
So I currently have 2 JoySticks On the screen, they will follow each of my thumbs fine but I can't figure out how to limit there movement and make them move around the centre point while following my fingers when my thumb goes out goes off of it while playing (like everyone joy stick).
Please no asset packs or "you can use the unity mobile assets".
I want to make it from scratch so I can learn and remember how to do it!
using UnityEngine;
using System.Collections;
public class JoyStick : MonoBehaviour
{
public bool JoyMoveIsTouched;
public bool JoyTurnIsTouched;
public GameObject JoyMove;
public GameObject JoyTurn;
public GameObject CenterMove;
public GameObject CenterTurn;
public float m_fMaxDistance;
public Vector3 TurnPos;
public Vector3 MovePos;
[SerializeField] private Camera UICam;
// Use this for initialization
void Start ()
{
TurnPos = JoyTurn.transform.position;
MovePos = JoyMove.transform.position;
}
// Update is called once per frame
void Update () {
JoyMoveIsTouched = false;
JoyTurnIsTouched = false;
Vector3 vCenterMove = CenterMove.transform.position;
Vector3 vCenterTurn = CenterTurn.transform.position;
foreach (var touch in Input.touches)
{
Ray ray = UICam.camera.ScreenPointToRay(touch.position);
RaycastHit hit;
if (JoyMove.collider.Raycast(ray, out hit, Mathf.Infinity))
{
JoyMoveIsTouched = true;
Vector3 vHitPoint = new Vector3(hit.point.x, hit.point.y, 0);
if (Vector3.Distance(JoyMove.transform.position, MovePos) > 0.2f)
{
JoyMove.transform.position = Vector3.MoveTowards(JoyMove.transform.position, MovePos, Time.deltaTime * 0.5f);
}
else
{
JoyMove.transform.position = Vector3.MoveTowards(JoyMove.transform.position, vHitPoint,
Time.deltaTime * 3);
}
}
else
{
//JoyMove.transform.position = Vector3.MoveTowards(JoyMove.transform.position, MovePos, Time.deltaTime * 7);
}
if(JoyTurn.collider.Raycast(ray, out hit, Mathf.Infinity))
{
JoyTurnIsTouched = true;
Vector3 vHitPoint = new Vector3(hit.point.x, hit.point.y, 0);
if (Vector3.Distance(JoyTurn.transform.position, TurnPos) > 0.2f)
{
JoyTurn.transform.position = Vector3.MoveTowards(JoyTurn.transform.position, TurnPos, Time.deltaTime*0.5f);
}
else
{
JoyTurn.transform.position = Vector3.MoveTowards(JoyTurn.transform.position, vHitPoint,
Time.deltaTime*3);
}
}
else
{
//JoyTurn.transform.position = Vector3.MoveTowards(JoyTurn.transform.position, TurnPos, Time.deltaTime * 7);
}
}
if(Input.touchCount < 1)
{
JoyMove.transform.position = Vector3.MoveTowards(JoyMove.transform.position, MovePos, Time.deltaTime * 7);
JoyTurn.transform.position = Vector3.MoveTowards(JoyTurn.transform.position, TurnPos, Time.deltaTime * 7);
}
}
}
That's my current script but it really isn't very good xD
I've been programming in C# and using unity for years, but for some reason I'm finding this really hard :(
Thanks in advance! :)
EDIT: BTW! the sprites (the game objects in the script) are NGUI Sprites with colliders on!
Your answer
Follow this Question
Related Questions
Joystick movement and turning 3rd person(Static Camera) 0 Answers
XR Descriptor not found 1 Answer
How to check whether you have swiped upwards on a mobile device - c# 1 Answer
Soft turn with joystick 0 Answers
Android MP4 Video Playback 2 Answers