So I’ve been working with Unity Android for the past few weeks and have been struggling with one problem over and over and over again. I’ve gone through the documentation and even browsed through the Penelope scripts but I can’t find a solution to this problem.
I’m working on the Acer Iconia A200 and testing with a basic unity script:
for(MoveFinger = 0; MoveFinger < Input.touchCount; ++MoveFinger){
if (Input.GetTouch(MoveFinger).phase == TouchPhase.Began){ //USE WHEN PORTING TO ANDROID
if (Physics.Raycast (moveray, FingerHit, 200, MoveLayer) && Spinning == false) {
if(FingerHit.collider.gameObject.tag == "ReturnNull"){
}
if(FingerHit.collider.gameObject.tag == "MoveLayer"){
StartClickTimer = true;
Debug.DrawLine(moveray.origin, FingerHit.point);
MoveTarget.position = FingerHit.point;
}
}
}
}
So the issue is this: it works perfectly when one finger is on the screen. When two fingers are on the screen it can’t differentiate between the old finger and the new finger and so it averages the distance between the two. Additionally, it occasionally can’t tell when a finger has been lifted from the screen and will read one finger as two.
What is a good workaround to this?
The penelope script referenced a concept called “latching”. How can I implement this?
Thanks in advance.
EDITED TO ADD NEWER/LONGER VERSION OF THE CODE
function Update(){
var MoveLayer = 1 << 8;
//Knowing the LerpDistance ensures constant Lerp Speed
var moveray = Camera.main.ScreenPointToRay (Input.GetTouch(MoveFinger).position);
var ClickRay = Camera.main.ScreenPointToRay (Input.GetTouch(MoveFinger).position);
var FingerHit : RaycastHit;
var ClickHit : RaycastHit;
var LerpDistance = Vector3.Distance(PlayerShip.transform.position, MoveTarget.position);
if(IsRotating == false){
RotateTarget.localEulerAngles = Vector3(0,0, PlayerShip.transform.position.x * 2);
}
IsRotating = false;
MoveTarget.position.x = Mathf.Clamp (MoveTarget.transform.position.x, -12, 12);
MoveTarget.position.y = Mathf.Clamp (MoveTarget.transform.position.y, -6, 6);
// After 10 seconds of damage has passed, start restoring the shield
DamageTimer += 1 * Time.deltaTime;
if (DamageTimer >= 10){
RestoreShield();
}
///If we're single clicking move at normal speed to the position
if(DoubleClicked == false && Spinning == false){
PlayerShip.transform.position.x = Mathf.Lerp(PlayerShip.transform.position.x, MoveTarget.position.x, Time.deltaTime / LerpDistance * ShipSpeed);
PlayerShip.transform.position.y = Mathf.Lerp(PlayerShip.transform.position.y, MoveTarget.position.y, Time.deltaTime / LerpDistance * ShipSpeed);
PlayerShip.transform.rotation = Quaternion.Lerp(PlayerShip.transform.rotation, RotateTarget.rotation, Time.deltaTime * ShipSpeed/2);
}
for(MoveFinger = 0; MoveFinger < Input.touchCount; ++MoveFinger){
if (Input.GetTouch(MoveFinger).phase == TouchPhase.Began){ //USE WHEN PORTING TO ANDROID
FirstFingerID = Input.GetTouch(MoveFinger);
MoveFinger = FirstFingerID.fingerId;
print(FirstFingerID.fingerId);
if (Physics.Raycast (moveray, FingerHit, 200, MoveLayer) && Spinning == false) {
if(FingerHit.collider.gameObject.tag == "ReturnNull"){
}
if(FingerHit.collider.gameObject.tag == "MoveLayer" && FirstFingerID.fingerId == 0){
StartClickTimer = true;
Debug.DrawLine(moveray.origin, FingerHit.point);
MoveTarget.position = FingerHit.point;
}
if(FirstFingerID.fingerId == 1){
print("I suck");
}
}
}
}
}
The script basically moves a target named “MoveTarget” to a place in the world, designated by the player’s finger touching the screen. Then the script Lerps the Ship model towards that target. Simple, but broken