0
public float speed = 15f;
public float mapWidth = 5f;
private Rigidbody2D rb;

private void FixedUpdate()
 {
     float x = Input.GetAxis("Horizontal") * Time.fixedDeltaTime * speed;
     Vector2 newPosition = rb.position + Vector2.right * x;
     newPosition.x = Mathf.Clamp(newPosition.x, -mapWidth, mapWidth);
     rb.MovePosition(newPosition);
 }

How do you change the code in FixedUpdate() be in touch control for MOBILE. so when I drag the object(my player) it will follow in the Horizontal axis only BUT! it will not go off the boundaries of the camera but also controllable width like in this code. If the number is high in mapWidth it will only move a little left and right.

9
  • What do you mean by "boundaries"?
    – Programmer
    Apr 9, 2018 at 6:23
  • the screen has a playable area. Like a ring.Square sprite thing. the player should not be able to go off that.
    – Justin
    Apr 9, 2018 at 6:26
  • You mean screen boundary? They don't go off the screen?
    – Programmer
    Apr 9, 2018 at 6:33
  • Yes. Yes that's it.
    – Justin
    Apr 9, 2018 at 6:41
  • Ok. See the answer from the duplicate. The first part is a non rigidbody solution. You need to read the second part of the answer which uses rigidbody.
    – Programmer
    Apr 9, 2018 at 6:43

1 Answer 1

1

The answer from this question shows how to move object and apply boundary on the screen with WorldToViewportPoint.

You can add touch support to it by adding Input.touches.deltaPosition.x and Input.touches.deltaPosition.y under the Input.GetAxis("Horizontal").

With both boundary and Input support added, below is what it should look like:

public float speed = 100;
public Rigidbody2D rb;

public void Update()
{
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");

    //Add touch support
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    {
        Touch touch = Input.touches[0];
        h = touch.deltaPosition.x;
        v = touch.deltaPosition.y;
    }

    //Move only if we actually pressed something
    if ((h > 0 || v > 0) || (h < 0 || v < 0))
    {
        Vector3 tempVect = new Vector3(h, v, 0);
        tempVect = tempVect.normalized * speed * Time.deltaTime;

        //rb.MovePosition(rb.transform.position + tempVect);

        Vector3 newPos = rb.transform.position + tempVect;
        checkBoundary(newPos);
    }
}

void checkBoundary(Vector3 newPos)
{
    //Convert to camera view point
    Vector3 camViewPoint = Camera.main.WorldToViewportPoint(newPos);

    //Apply limit
    camViewPoint.x = Mathf.Clamp(camViewPoint.x, 0.04f, 0.96f);
    camViewPoint.y = Mathf.Clamp(camViewPoint.y, 0.07f, 0.93f);

    //Convert to world point then apply result to the target object
    Vector3 finalPos = Camera.main.ViewportToWorldPoint(camViewPoint);
    rb.MovePosition(finalPos);
}
2
  • 1
    Thank you so much! This work and I just removed the Vertical control for this. now its really what I needed Thank you so much.
    – Justin
    Apr 9, 2018 at 8:02
  • Glad I was able to help
    – Programmer
    Apr 9, 2018 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.