I’m sorry I’m creating a post here for this but I just can’t seem to find anyone who has an actual answer, so i’m trying a fresh question:
Is there anyone who has managed to make the dynamic joystick thing happen?
I have managed to modify the original standard (mobile) kit code shipped with unity so that it places the joystick where i put my finger. Trouble is that it only puts the graphic there. The joystick’s ZERO is always at the x,y=zero of the screen. e.g. anywhere i click, my joystick points up+right.
Any ideas?
Thanks in advance.
(hive mind rulez)
Posting my solution here:
joystick.js to be attached to the joystick object
#pragma strict
var thumb : GUITexture;
var base : GUITexture;
var origin : Vector2;
var baseWidth : int;
var baseHeight : int;
var position : Vector2;
var fadeInLeft : boolean;
var fadeInRight : boolean;
var JoyID : int;
static var joysticks : Joystick[]; // A static collection of all joysticks
function Start () {
thumb = GetComponent(GUITexture);
baseWidth = base.pixelInset.width;
baseHeight = base.pixelInset.height;
}
function Update () {
if(fadeInLeft){
gameObject.Find("LeftBase").GetComponent(GUITexture).color.a=Mathf.Lerp(gameObject.Find("LeftBase").GetComponent(GUITexture).color.a,1.0,Time.deltaTime*8);
gameObject.Find("LeftJoystick").GetComponent(GUITexture).color.a=Mathf.Lerp(gameObject.Find("LeftJoystick").GetComponent(GUITexture).color.a,1.0,Time.deltaTime*8);
}else if(!fadeInLeft){
gameObject.Find("LeftBase").GetComponent(GUITexture).color.a=Mathf.Lerp(gameObject.Find("LeftBase").GetComponent(GUITexture).color.a,0.0,Time.deltaTime);
gameObject.Find("LeftJoystick").GetComponent(GUITexture).color.a=Mathf.Lerp(gameObject.Find("LeftJoystick").GetComponent(GUITexture).color.a,0.0,Time.deltaTime);
}
if(fadeInRight){
gameObject.Find("RightBase").GetComponent(GUITexture).color.a=Mathf.Lerp(gameObject.Find("RightBase").GetComponent(GUITexture).color.a,1.0,Time.deltaTime*8);
gameObject.Find("RightJoystick").GetComponent(GUITexture).color.a=Mathf.Lerp(gameObject.Find("RightJoystick").GetComponent(GUITexture).color.a,1.0,Time.deltaTime*8);
}else if(!fadeInRight){
gameObject.Find("RightBase").GetComponent(GUITexture).color.a=Mathf.Lerp(gameObject.Find("RightBase").GetComponent(GUITexture).color.a,0.0,Time.deltaTime);
gameObject.Find("RightJoystick").GetComponent(GUITexture).color.a=Mathf.Lerp(gameObject.Find("RightJoystick").GetComponent(GUITexture).color.a,0.0,Time.deltaTime);
}
var count = Input.touchCount;
for(var i : int = 0;i < count; i++){
var touch : Touch = Input.GetTouch(i);
var getTouchPosition : Vector2 = touch.position;
//finger down
if (getTouchPosition.x < Screen.width/2 && getTouchPosition.y < Screen.height/3 && name == "LeftJoystick" && touch.phase == TouchPhase.Began){
origin = getTouchPosition;
base.pixelInset.x = origin.x-baseWidth/2;
base.pixelInset.y = origin.y-baseHeight/2;
thumb.pixelInset.x = origin.x-thumb.pixelInset.width/2;
thumb.pixelInset.y = origin.y-thumb.pixelInset.height/2;
fadeInLeft=true;
JoyID = 1;
}
if (getTouchPosition.x > Screen.width-(Screen.width/2) && getTouchPosition.y < Screen.height/3 && name == "RightJoystick" && touch.phase == TouchPhase.Began){
origin = getTouchPosition;
base.pixelInset.x = origin.x-baseWidth/2;
base.pixelInset.y = origin.y-baseHeight/2;
thumb.pixelInset.x = origin.x-thumb.pixelInset.width/2;
thumb.pixelInset.y = origin.y-thumb.pixelInset.height/2;
fadeInRight=true;
JoyID = 2;
}
//finger move
if (getTouchPosition.x < Screen.width/2 && getTouchPosition.y < Screen.height/3 && name == "LeftJoystick" && touch.phase == TouchPhase.Moved){
thumb.pixelInset.x = Mathf.Clamp((getTouchPosition.x-thumb.pixelInset.width/2),origin.x-(thumb.pixelInset.width),origin.x);
thumb.pixelInset.y = Mathf.Clamp((getTouchPosition.y-thumb.pixelInset.height/2),origin.y-(thumb.pixelInset.height),origin.y);
position.x = (getTouchPosition.x-base.pixelInset.x-(thumb.pixelInset.width/2)) / baseWidth;
position.y = (getTouchPosition.y-base.pixelInset.y-(thumb.pixelInset.width/2)) / baseHeight;
}
if (getTouchPosition.x > Screen.width-(Screen.width/2) && getTouchPosition.y < Screen.height/3 && name == "RightJoystick" && touch.phase == TouchPhase.Moved){
thumb.pixelInset.x = Mathf.Clamp((getTouchPosition.x-thumb.pixelInset.width/2),origin.x-(thumb.pixelInset.width),origin.x);
thumb.pixelInset.y = Mathf.Clamp((getTouchPosition.y-thumb.pixelInset.height/2),origin.y-(thumb.pixelInset.height),origin.y);
position.x = (getTouchPosition.x-base.pixelInset.x-(thumb.pixelInset.width/2)) / baseWidth;
position.y = (getTouchPosition.y-base.pixelInset.y-(thumb.pixelInset.width/2)) / baseHeight;
}
//finger leave
if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){
thumb.pixelInset.x = origin.x-thumb.pixelInset.width/2;
thumb.pixelInset.y = origin.y-thumb.pixelInset.height/2;
position = Vector2.zero;
if(name=="LeftJoystick" && JoyID == 1){fadeInLeft=false;}
else if(name=="RightJoystick" && JoyID == 2){fadeInRight=false;}
}
}
}