How to move 2D Object within camera view boundary

后端 未结 4 1598
感动是毒
感动是毒 2020-12-21 23:47

I have a scene that my camera doesn\'t follow my player. When player reaches the end of camera I want player to can\'t go further (out of camera view). How can I do this?

相关标签:
4条回答
  • 2020-12-22 00:06

    You need to limit your transform's position based on the edges of the camera. Here is an answer describing the different coordinate systems in unity

    You're probably looking to do something like this:

    float xMin = Camera.main.ViewportToWorldPoint(Vector3.zero).x;
    float xMax = Camera.main.ViewportToWorldPoint(Vector3.one).x;
    
    Vector3 currentPos = transform.position;
    float dx = Input.GetAxis ("Horizontal") / 100 * speed;
    Vector3 desiredPos = new Vector3(currentPos.x + dx, currentPos.y, currentPos.z);
    
    Vector3 realPos = desiredPos;
    
    if(desiredPos.x > xMax)
        realPos.x = xMax;
    else if(desiredPos.x < xMin)
        realPos.x = xMin;
    
    transform.position = realPos;
    

    Read up here for more info on ViewportToWorldPoint(), it's extremely useful to become comfortable with the different coordinate spaces and how you can convert between them.

    0 讨论(0)
  • 2020-12-22 00:18

    I noticed you used a Collider2D. You should be using Rigidbody2D.MovePosition instead of transform.Translate or you'll likely run into issues when transform.Translate is used.

    1.Take the final move position and convert it to new position in ViewPortPoint with Camera.main.WorldToViewportPoint

    2.Apply a limit with Mathf.Clamp to the result in #1.

    3.Convert the ViewPortPoint back to world point with Camera.main.ViewportToWorldPoint.

    4.Finally, move it with Rigidbody2D.MovePosition.


    The code below is modified from this answer to include restriction to screen boundary.

    Move without Rigidbody:

    Use only if collision and physics are NOT required:

    public float speed = 100;
    public Transform obj;
    
    public void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
    
        //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;
    
    
            Vector3 newPos = obj.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
        obj.position = Camera.main.ViewportToWorldPoint(camViewPoint);
    }
    

    Move Object with Rigidbody2D:

    Use if collision and physics are required:

    public float speed = 100;
    public Rigidbody2D rb;
    
    public void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
    
        //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);
    }
    
    0 讨论(0)
  • 2020-12-22 00:19

    image not respond . but you can check player location

    x = Input.GetAxis ("Horizontal") / 100 * speed;
    if(gameobject.transform.x > someValue)
        x=0
    

    gameobject will be OBJECT in scene that u attach class to it.

    another way is place 2 empty gameobject with collider as invisibleWall and get collider to player

    0 讨论(0)
  • 2020-12-22 00:22

    Possible solution is:

    1.Get the coordinates of your screen cornes (top left, top right, bottom left, bottom right). You can get this coordinates using Screen.height and Screen.width.

    2.Convert this coordinates using the camera you need with Camera.ScreenToWorldPoint.

    3.Get your player coordinates and check that they are inside the rect which is formed by 4 coordinates of the screen corners.

    0 讨论(0)
提交回复
热议问题