Ship Going Outside Of Camera Boundary

Hello,

I am following the Laser Defender portion of the course and tried to implement the ViewportToWorldPoint technique for the boundaries like in the video, but unfortunately my player sprite isn’t quite doing what it is supposed to.

It stays on screen to the top and to the left (though it doesnt go right up to the edge), but it goes off screen on the top and the bottom.

I tried to do some debugging and rewatched the video a couple times but it seems my code is identical so I am not really sure what could be going wrong here.

code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
{
    [SerializeField] float moveSpeed = 0.5f;
    Vector2 rawInput;

    Vector2 minBound;  // bottom left of viewport
    Vector2 maxBound;  // top right of viewport

    void Start()
    {
        InitBounds();
    }


    void Update()
    {
        Move();
    }

    void InitBounds()
    {
        Camera mainCamera = Camera.main;
        minBound = mainCamera.ViewportToWorldPoint(new Vector2(0, 0));
        maxBound = mainCamera.ViewportToWorldPoint(new Vector2(1, 1));   // 5.06, 9

    }


    void Move()
    {
        Vector2 delta = rawInput * moveSpeed * Time.deltaTime;
        Vector2 newPos = new Vector2();
        newPos.x = Mathf.Clamp(transform.position.x + delta.x, minBound.x, maxBound.x);  // adds the current x and y position of player with the amount we are going to move
        newPos.y = Mathf.Clamp(transform.position.y + delta.y, minBound.y, maxBound.y);  // and clamps it to not be above the boundary limits
        transform.position = newPos;
    }


    void OnMove(InputValue value)
    {
        rawInput= value.Get<Vector2>();
    }
}

Based on some debugging, my minBound and maxBound are 5.06 and 9 (and their negative counterparts). This holds up when playtesting as well as that is where the values stop moving, but the -9 goes past the camera boundary, and the positive 5.06 goes past the camera boundary.

My camera size is 9 and is set to orthographic projection, I don’t really know what else to check at this stage or why this isnt working, any thoughts would be appreciated!

For the time being I got around it by hardcoding some addition in but this feels very dirty and Im really at a loss as to why the boundary isn’t working as intended. It is interesting with the below math that the maxbound.x and minbound.y are identical (negative sign aside both are absolute value 1.5) and minbound.x and minbound.y are also identical (0.5)

I cant really draw any firm conclusions from that at the moment but my gut tells me that is a telling sign of something not calculating quite right.

For reference, here is my updated relevant code

newPos.x = Mathf.Clamp(transform.position.x + delta.x, (float)(minBound.x - 0.5), (float)(maxBound.x - 1.5));
newPos.y = Mathf.Clamp(transform.position.y + delta.y, (float)(minBound.y + 1.5), (float)(maxBound.y + 0.5)); 

As you can see I basically just tested out some floats until the ship stayed within the intended bounds, it does work but not sure if this will cause me problems down the line (I doubt it since its just the boundary), but even so I would like to better understand why I am apparently having some edge case occur that isn’t expected.

EDIT: continuing through the video I guess I just implemented my own padding without realizing that was coming up next. I still don’t really understand why my player wasnt sticking to the boundaries before the padding like the instructor was, but with the padding things seem to be working (though I would still like to try and understand why my original outcome before padding was different)

If your left and right bounds are not symmetrical, the first thing I’d check is the position of the camera. It may be slightly off center and resetting the position to (0,0) might fix the issue.

Hopefully it will be as simple as that.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms