In Unity
, I'm trying to make my sprite go offscreen to the left and have it appear offscreen on the right or vice versa. I have my sprite move left or right (depending on user input) constantly for my gameplay by the way.
I take into account that the sprite needs to be fully offscreen before having it appear on the right side.
Here's my current code:
void Start ()
{
minXValueWorld = Camera.main.ScreenToWorldPoint(new Vector3(0,0,0)).x;
maxXValueWorld = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0)).x;
playerSize = this.GetComponent<Renderer>().bounds.size;
}
void Move()
{
if (inputLeft)
{
this.transform.position -= new Vector3(speed * Time.deltaTime, 0, 0);
}
else
{
this.transform.position += new Vector3(speed * Time.deltaTime, 0, 0);
}
}
void OffscreenCheck()
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
Vector3 maxWorldXWithPlayerSize = Camera.main.WorldToScreenPoint(new Vector3(maxXValueWorld,0,0) + playerSize/2);
Vector3 minWorldWithPlayerSize = Camera.main.WorldToScreenPoint(new Vector3(minXValueWorld,0,0) - playerSize/2);
if (screenPos.x < minWorldWithPlayerSize.x)
{
this.transform.position = new Vector3(maxWorldXWithPlayerSize.x, this.transform.position.y, this.transform.position.z);
}
if (screenPos.x > maxWorldXWithPlayerSize.x)
{
this.transform.position = new Vector3(minWorldWithPlayerSize.x, this.transform.position.y, this.transform.position.z);
}
}
Then the Move
and OffscreenCheck
are called in the Update
function in order.
The problem with this code is that once my sprite goes fully offscreen on the left, it appears on the right offscreen, but it does not move left anymore.
Instead, it just teleports to the left or right offscreen positions. I do not see it move left or right across the screen anymore because of this.
I'm pretty sure that my code logic is just off. Does anyone know how to fix this issue?
Thanks
new Vector3(maxXValueWorld,0,0) + playerSize/2
. I would think that would make more sense asnew Vector3(maxXValueWorld + playerSize/2,0,0)
. The way you have it, I can imagine some strangeness going on transforming to screen coordinates, in which thex
value doesn't wind up where you thought it should be. – Peter Duniho-19
and424
(based on my screen aspect ratio, at least). It just switches between those x-positions which is the problem because those are offscreen positions already. I'm not sure but I think the problem is with my logic in myif
statements. – areszif
blocks, when I change themaxWorldXWithPlayerSize.x
to simplymaxXValueWorld
, andminWorldWithPlayerSize.x
tominXValueWorld
, the object appears on the other side of the screen successfully. But what I want is to have it appear from the offscreen so that the object doesn't just appear out of nowhere with its half showing (since the pivot point of the sprite is on the center) – aresz