I’m trying to get a circle to bounce off the screen edge, here’s the code I’m using
rigidbody2D.AddForce (randomVector * 30f);
Vector3 pos = Camera.main.WorldToViewportPoint (transform.position);
if (pos.x < 0.0) {
//Debug.Log ("I am left of the camera's view.");
randomVector = Vector3.Reflect (randomVector, Vector3.right);
}
if (1.0 < pos.x) {
//Debug.Log ("I am right of the camera's view.");
randomVector = Vector3.Reflect (randomVector, Vector3.left);
}
if (pos.y < 0.0) {
//Debug.Log ("I am below the camera's view.");
randomVector = Vector3.Reflect (randomVector, Vector3.up);
}
if (1.0 < pos.y) {
//Debug.Log ("I am above the camera's view.");
randomVector = Vector3.Reflect (randomVector, Vector3.down);
}
And it detects the screen edge fine, but it’s not changing the vector to move in the correct reflected direction.