I have the script pretty much done, there are probably redundant things in the script but I’m new to unity and not really concerned with the most efficient way to do things (at least not yet), the problem is at the bottom of the code, hopefully you can help me out.
It needs to be able to handle left and right inputs independent of each other, because I’m trying to do a twin stick shooter type thing.
Thanks in advance!
var fingerDown : boolean = false;
var startPos : Vector2;
var fingerMoved : boolean = false;
var direction : Vector2;
var directionNormal : Vector3;
var directionRot : Vector2;
var directionSpeed : float;
var player : GameObject;
var maxSpeed : float = 5;
var touchAmount;
function Update () { //Preface: using the word touch as a noun is funny and creepy at the same time
for(var i=0 ; i<Input.touchCount Input.GetTouch(i).position.x < Screen.width/2 ; i++){ //if there is at least one touch on the LEFT of the screen
if(!fingerDown){ //if this is a new touch
startPos = Input.GetTouch(i).position; //save the position
fingerDown = true; //all touches after arn't new
Debug.Log("touch down.");
} else { //if there this is NOT a new touch
if(TouchPhase.Moved){ //if the finger moved
fingerMoved = true; //save that for later maybe
direction = Input.GetTouch(i).position - startPos; //get the vector
directionSpeed = Mathf.Clamp(direction.magnitude,-maxSpeed,maxSpeed); //and speed of the vector
directionRot = direction.normalized; //and rotation of the vector
directionNormal = Vector3(directionRot.x,0,directionRot.y) * directionSpeed; //save a variable for movement stuff
Debug.DrawRay(player.transform.position,directionNormal,Color.blue);
}
}
}
[I DONT KNOW WHAT TO PUT HERE]{ //if there is no touch on the LEFT side of the screen
fingerDown = false; //reset the variable
fingerMoved = false; //reset this variable too
Debug.Log("touch up.");
}
}
Ok, I figured it out. It basically involves angrily interrogating the amount and position of inputs on screen until you have exhausted all possible variables. Here is the code I’m using if anyone has a similar problem. if you have a better solution please post it
var fingerDown : boolean = false;
var startPos : Vector2;
var fingerMoved : boolean = false;
var direction : Vector2;
var directionNormal : Vector3;
var directionRot : Vector2;
var directionSpeed : float;
var player : GameObject;
var maxSpeed : float = 5;
var touchAmount;
function Update () { //Preface: using the word touch as a noun is funny and creepy at the same time
for(var i=0 ; i<Input.touchCount ; i++){ //for as long as there are >0 fingers on screen
if(Input.GetTouch(i).position.x < Screen.width/2){ //if the finger is on the LEFT
if(!fingerDown){ //if this is a new touch
startPos = Input.GetTouch(i).position; //save the position
fingerDown = true; //all touches after arn't new
Debug.Log("touch down.");
} else { //if there this is NOT a new touch
if(TouchPhase.Moved){ //if the finger moved
fingerMoved = true; //save that for later maybe
direction = Input.GetTouch(i).position - startPos; //get the vector
directionSpeed = Mathf.Clamp(direction.magnitude,-maxSpeed,maxSpeed); //and speed of the vector
directionRot = direction.normalized; //and rotation of the vector
directionNormal = Vector3(directionRot.x,0,directionRot.y) * directionSpeed; //save a variable for movement stuff
Debug.DrawRay(player.transform.position,directionNormal,Color.blue);
}
}
}
if(Input.GetTouch(i).position.x > Screen.width/2 Input.touchCount <= 1 fingerDown){ //if the touch is on the RIGHT and it's the only touch AND theres variables saved
fingerDown = false; //reset this variable
fingerMoved = false; //reset this variable too
Debug.Log("touch up.");
}
}
if(Input.touchCount <= 0 fingerDown){ //if there are no touches
fingerDown = false; //reset this variable
fingerMoved = false; //reset this variable too
Debug.Log("touch up.");
}
}