- Home /
How to reach limit line, keep moving, even past over the touch position?
Hello, I'm beginner,
My game player detect touch position and go to this direction in the room. If one touch detect player must to go along touch point until reach the wall. It means if player was on left top corner, touch position is detected on center, player must to direct touch center position, but until reach the right bottom corner.
I don't know how to make this?
Here is the code:
public float playerSpeed;
private Vector2 directionPosition;
private Touch touch;
private Camera mainCamera;
private Vector2 xLimit;
private Vector2 yLimit;
private float xPos;
private float yPos;
void Start () {
mainCamera = (Camera) GameObject.FindObjectOfType(typeof(Camera));
xLimit = new Vector2(-ScreenFitter.wallVerticalPositionX, ScreenFitter.wallVerticalPositionX);
yLimit = new Vector2(-ScreenFitter.wallHorizontalPositionY, ScreenFitter.wallHorizontalPositionY);
}
void Update () {
if (Input.touchCount > 0) {
touch = Input.GetTouch(0);
directionPosition = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10.0f));
transform.position = Vector3.Lerp(transform.position, directionPosition, playerSpeed*Time.deltaTime);
if(transform.position.x <= xLimit.x || transform.position.x >= xLimit.y){
xPos = Mathf.Clamp(transform.position.x, xLimit.x, xLimit.y);
transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
}
if(transform.position.y <= yLimit.x || transform.position.y >= yLimit.y){
yPos = Mathf.Clamp(transform.position.y, yLimit.x, yLimit.y);
transform.position = new Vector3(transform.position.x, yPos, transform.position.z);
}
}
}
And if touch far, player can't go to the point. During the way player stops. What I did wrong?
Thank in advance.
Comment