Hello,
I would like to know how i can get the X position of the very left edge of the screen,
so i can make background repeat itself once it gets out of the camera’s view.
what i try to make:
as soon as a background leaves the camera’s view on the left, it’s being pasted to the right of the camera.
Perspective camera: use Camera.ScreenPointToRay use it with any vector where x==0 (for example new Vector2(0,0)) and then continue that ray until intersects plane which contains your background (line-plane intersection is easy to calculate manually).
Orthographic camera can use simpler way: Camera.ScreenToWorldPoint with same vector (x==0) as parameter.
From result get .x component.
I am using it the way you say, this is my script. but it bugs
using UnityEngine;
using System.Collections;
public class Continue : MonoBehaviour {
private Camera cam;
public Transform target;
private Vector3 positionVector;
private float PosVectorXAxis;
// Use this for initialization
void Start () {
cam = Camera.main;
}
// Update is called once per frame
void Update () {
Vector3 viewPos = cam.WorldToViewportPoint(transform.position);
PosVectorXAxis = 768.1f + viewPos.x; // i tried this to calculate the X pos of where is needs to be placed
positionVector = new Vector3(PosVectorXAxis, 39f, 2.5f); // this is where the background should be placed once he is too far to the left
//transform.Translate(Vector3.left * 5);
if (viewPos.x < 0f)
{
Debug.Log("i am to the left of the Camera");
transform.position = positionVector;
}
if(viewPos.x > 1.0)
{
Debug.Log("I am to the right of the camera");
}
}
void FixedUpdate()
{
}
}
somehow it does detact if it’s to the left, but the placement on the right is not doing well, it looks like it’s not updating the position, because once it goes to the left of the screen he puts it at a steady X position, so not on the position relative to the camera.
I see you’re getting coordinate using WorldToViewportPoint - that’s not screen point. It’s world point relative to camera’s world point. And judging from 768.1f+ you mean to add pixel size(screen coordinates). If I’m right, you should use WorldToScreenPoint instead.
Also… I don’t see ScreenToWorldPoint as I’ve described.
In case of orthogonal camera I think it should look like this:
float offsetX = 768.1f; // This should contain size of your background image by X axis in pixels on screen
Vector3 screenPos = cam.WorldToScreenPoint(transform.position);
if(screenPos.x<0) //if you're moving camera right
{
screenPos.x += offsetX;
transform.position = cam.ScreenToWorldPoint(screenPos);
}
else if(screenPos.x > Screen.width)//If you're moving camera left
{
screenPos.x -= offsetX;
transform.position = cam.ScreenToWorldPoint(screenPos);
}
if you have world size of background (that’s even better) you can just use transform.position.x+=offsetX;(and -=offsetX) instead without any cam.ScreenToWorldPoint. It’s safer, faster, and overall better. However I’m 99% sure in that case 768.1f is far too much.
This script should be on the Background itself right? if so, it doesn’t work somehow.
here are a few screenshots
Um,yeah it doesn’t work. I’m having trouble thinking off algorithm that uses only 2 backgrounds (it’s certainly possible, it’s just I’m not in best shape), so let’s go with what is simple and works:
Place 3 backgrounds to they’re fitting right. Find difference between their X and set it to worldSizeX variable in inspector. Make sure you set worldSizeX to same value on all 3. Attach this script to all 3 of them:
//file MoveBackground.cs
using UnityEngine;
public class MoveBackground: MonoBehaviour
{
public float worldSizeX = 50;//Set this to you world-size of background.
void Update()
{
//Calculates relative to camera position. That's simplest way.
float cameraX = Camera.main.transform.position.x;
Vector3 pos = transform.position;
//Note: I compare transform.position assuming it's in middle of background texture. If it's on left edge, add +worldSizeX/2 in both ifs before >
if(pos.x - cameraX > 1.5f * worldSizeX)
{
pos.x -= 3*worldSizeX;
transform.position = pos;
}
else if (pos.x - cameraX < -1.5f * worldSizeX)
{
pos.x += 3*worldSizeX;
transform.position = pos;
}
}
}
P.S. I use Camera.main, but you can replace it with cam you used.
Thanks a bunch! that one worked!