Hello,
I used to work with Unity but now I did not use it for a while, and I kinda forget some things how it worked ![]()
I am making a 2D game for iOS and Android and you have to drag pawns around the playing board.
Now I wrote a script that works perfectly for 1 pawn, but if I add other pawns they will all move at the same time. Even though I am raycasting which pawn is touched, and only moving the “this” object.
Here you can see my script, it is added to all the pawns, and now they all move, but i want only to move the one i touched…
public var speed:int = 17;
private var x:float;
private var y:float;
private var hit: RaycastHit;
public var pawnSelected:boolean = false;
function Update () {
for (var touch : Touch in Input.touches) {
// and only if the touch moved since last update
var ray = Camera.main.ScreenPointToRay (touch.position);
var hit : RaycastHit;
if(touch.phase == TouchPhase.Began Physics.Raycast(ray, hit)) {
if(hit.collider.gameObject.tag == "Pawn"){
pawnSelected = true;
}
}
if(Input.touchCount > 0){ // Only work with the first touch
if(Input.touchCount == 1)
{
if (Input.touches[0].phase == TouchPhase.Moved)
{
if(pawnSelected){
x = Input.touches[0].deltaPosition.x * speed * Time.deltaTime;
y = Input.touches[0].deltaPosition.y * speed * Time.deltaTime;
this.transform.Translate(new Vector3(x, 0, y));
}
}
}
}
if(touch.phase == TouchPhase.Ended ) {
if(pawnSelected){
pawnSelected = false;
}
}
}
}