Hello!
I want to create a simple script that allows the user to walk to the left if the left side of the screen is touched, and ofcourse walk to the right if the right side is touched.
I would think my solution should work but it doesn’t move like he should.
I know my touchscript worked because I tried it out using another plane that you can touch to move.
Right now regardless of where I touch the screen, it will walk to the left, if I try to change the hit.point.x to greater or less than, it will just do the same but opposite.
Am I doing this wrong or why is this happening?
Ps. I’m trying it on an iphone.
here’s the script:
#pragma strict
var MovementSpeed: float;
function Start() {
MovementSpeed = 10f;
}
function Update() {
if (Input.touchCount > 0) {
var ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
if (hit.point.x > Screen.width / 2) {
transform.Translate(Vector2.right * Time.deltaTime * MovementSpeed);
}
if (hit.point.x < Screen.width / 2) {
transform.Translate(Vector2.right * Time.deltaTime * MovementSpeed * -1);
}
}
}
}