- Home /
Touch Crosshair problem.
I have a game in whici I use a joystick and with my other finger I can move around in an invisible rectangle on the screen an empty game object to which I have attached a crosshair.
It is working fine as long as I'm touching in that rectangle but the moment I lift my finger from that area , the object doesn't move anymore in space, because nothing is touched in that area. I need somehow to save it's position on the screen even when I lift my fingers :)
Any sugestions ?
// public variables
public var myobject : GameObject; //the empty game object that the turret and cannon wil be following
public var range : float = 10.0; // range of the object in the z axis
public var moveTouchPad : Joystick;
//private variables
//public static var TargetingArea2: Rect = new Rect(Screen.width - (Screen.width*0.9) , Screen.height*0.4, Screen.width, Screen.height/2); //tank cannot aim all the way up
public static var TargetingArea1: Rect = new Rect(0, Screen.height * 0.5, Screen.width, Screen.height/2); //places where the touch is read
var lastpos = Vector3.zero;
public var color: Color = Color.red;
// in this script we put the empty game object that will always be on the mouse position and is the object that our turret will be actually
// rotating towards
function Start(){
Screen.showCursor = false;
}
function Update () {
for (var touch : Touch in Input.touches){
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {
if(TargetingArea1.Contains(touch.position) /*|| TargetingArea2.Contains(touch.position) */){
//myobject.transform.position = ray.GetPoint(range)
myobject.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, range ));
}
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
if(TargetingArea1.Contains(touch.position){
/// i need it the crosshair to keep the same coordinates on screen even when i lift my fingers from this area
}
////
}
}
}
I found another aproach on the answers around here, something with finger ID's. The following code bellow works only if I first touch the joystick and then the other part of the screen, but the moment It's much more gameplay friendly without usin rectangles as you can move more freely on the screen expect for the exact surface you don't want it to go.
I take my ai$$anonymous$$g finger away it goes back on top of the joystick. It's something about their order and how they increment their ID's.
function Guncontroll() {
var ray: Ray;
for (var touch : Touch in Input.touches){
if(Input.touchCount > 0 && Input.GetTouch(0).fingerId != moveTouchPad.lastFingerId){
myobject.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, range ));
}
if(Input.touchCount > 1 && Input.GetTouch(0).fingerId == moveTouchPad.lastFingerId){
myobject.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, range ));
}
}
}
Answer by jacobschellenberg · Aug 01, 2013 at 04:10 PM
Perhaps have the Crosshairs position be that of the finger position? Otherwise it seems you would always have to touch the Crosshair to move it, rather then the crosshair being where your finger is.