- Home /
Found the answer myself
Position set way off / Different coordinate system problem
I'm beginning to make a game and I've encountered a small problem. I know this may be related to Unity's different coordinate system but please help, I'm new to this. Here's my simple script:
using UnityEngine;
using System.Collections;
public class Players : MonoBehaviour {
public GameObject player1;
public GameObject player2;
private Rigidbody2D rb1;
private Rigidbody2D rb2;
void Start () {
rb1 = player1.GetComponent<Rigidbody2D> ();
rb2 = player2.GetComponent<Rigidbody2D> ();
}
void Update () {
foreach (Touch t in Input.touches) {
if (t.position.x > Screen.width / 2) {
rb1.position=(new Vector2(rb1.position.x,t.position.y));
} else {
rb2.position=(new Vector2(rb2.position.x,t.position.y));
}
}
}
}
It does half of what it's supposed to do when I test it in Unity Remote on my phone. The players are moved accordingly, meaning they stay on the same X coordonate and the Y changes,but they go WAY off the camera screen, the Y is way too big. I expected the touch position on the phone to be similar to the position on the camera, if that makes sense. How can this difference in coordinate system be solved?
Finally found the answer THAN$$anonymous$$S for the long waiting time, made me struggle a whole day while adding other features.
For those who need the answer, look at the Camera.ScreenToWorldPoint function