Hi, I’m doing a 2D arcade game for Android devices and I’m having an issue with Vector3.back.
What I’m trying to achive is that the player avatar moves to the part of the screen that the player had touched. Vector3.left, .right and .forward are working nice but when I use Vector3.back or -Vector3.forward the object doesn’t moves to the touched position.
The script I’m using is the following one (if someone knows a better and cheaper way to do this I’d be pleased to know :D):
if (mobileControls == true) {
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) {
double halfXScreen = Screen.width / 2.0;
double halfZScreen = Screen.height / 2.0;
Vector3 touchPosition = Input.GetTouch(0).position;
if(touchPosition.x < halfXScreen) { //works nice
gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime);
}
else if (touchPosition.x > halfXScreen) { //works nice
gameObject.transform.Translate(Vector3.right * speed* Time.deltaTime);
}
if(touchPosition.z < halfZScreen) { //works nice
gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
else if (touchPosition.z > halfZScreen) { //doesn't works :(
gameObject.transform.Translate(Vector3.back * speed * Time.deltaTime);
}
}
I’m using Unity 5.4.1f1 Personal Edition (64 bits)