How would I make screen wrapping work with all aspect ratios/resolutions?

Hi guys I made this basic script here, I’m following a tutorial. This script works but with different resolutions and such, it kinda breaks. If I used this script on a 1080p screen it wouldn’t let me move.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

public float speed = 7;

float screenHalfWidthInWorldUnits = 9.5f;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
float inputX = Input.GetAxisRaw (“Horizontal”);
float velocity = inputX * speed;
transform.Translate (Vector2.right * velocity * Time.deltaTime);

if (transform.position.x < -screenHalfWidthInWorldUnits) {
transform.position = new Vector2 (screenHalfWidthInWorldUnits, transform.position.y);
}

if (transform.position.x < screenHalfWidthInWorldUnits) {
transform.position = new Vector2 (-screenHalfWidthInWorldUnits, transform.position.y);
}
}

Rather than just have fixed value for the screen width, on awake or start, set that value to the actual value of the screen width. Screen.width

Please use code tags to make your code much more readable.

I think the only problem is that you’ve hard-coded the screen size. Use Screen.width. Dangit, sniped by @zombiegorilla . :slight_smile: