-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenWrapper.cs
More file actions
71 lines (58 loc) · 2.13 KB
/
Copy pathScreenWrapper.cs
File metadata and controls
71 lines (58 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using UnityEngine;
public class ScreenWrapper : MonoBehaviour
{
//public float maxX = 9.5f;
//public float maxY = 5.5f;
private bool hasRb;
private Rigidbody2D rb;
//0.1 of the view size
[SerializeField] protected float baseMargin;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody2D>();
if (rb)
hasRb = true;
}
// Update is called once per frame
void Update()
{
float xMargin = baseMargin + (hasRb ? Mathf.Abs(rb.linearVelocity.x) * 0.1f : 0.0f);
float yMargin = baseMargin + (hasRb ? Mathf.Abs(rb.linearVelocity.y) * 0.1f : 0.0f);
//this is the width of the screen in world units (it depends on the camera settings)
//add a margin so the wrapping area is slightly larger than the camera view and the asteroids
//exit the screen before teleporting on the other side
float screenWidth = Camera.main.orthographicSize * Camera.main.aspect * 2;
float screenHeight = Camera.main.orthographicSize * 2;
//I can't assign a vector component to a transform directly so I use a temporary variable
//even if most of the times won't be changes
Vector2 newPosition = transform.position;
//check all the margin
if (transform.position.x > (screenWidth + xMargin) / 2)
{
newPosition.x = -(screenWidth + baseMargin)/ 2;
}
if (transform.position.x < -(screenWidth + xMargin) / 2)
{
newPosition.x = (screenWidth + baseMargin) / 2;
}
if (transform.position.y > (screenHeight + yMargin) / 2)
{
newPosition.y = -(screenHeight + baseMargin)/ 2;
}
if (transform.position.y < -(screenHeight + yMargin) / 2)
{
newPosition.y = (screenHeight + baseMargin) / 2;
}
//assign it to the transform
transform.position = newPosition;
}
public void SetBaseMargin(float bm)
{
baseMargin = bm;
}
public float GetBaseMargin()
{
return baseMargin;
}
}