I have a map (2d) which is about 5 times the size of the screen area. I can touch drag the map. However the user must only be able to drag it as far as the map is still showing. So the empty area outside map isn't showing.
So my question is: which of the two methods are best:
1) have colliders at the side of the screen and then put colliders on each side of the map. Then checking for collision and if so move the map so its collider does not touch the screen collider.
Or
2) check for the position of the map (i have an adjustable screen size according to device) and then stop the drag if it gets dragged further than visibility of screen. Using coordinates and math.
Will it take up more space (the ios/android) file if I use solution 1?
Thanks.
Related
I'm trying to display some Particles and Slider (like a health bar) over a GameObject in Unity 2020.3.26, but even if in the Scene it's showing like I want, when playing, the canvas seems to orient itself "too much" and I don't know how to make it so both the Particles and Slider stays in place.
As shown in the images, I've put a canvas in a GameObject "Aimable Target Body", and when I look at the center of the object it works as I want.
But if I start looking away, instead of staying centered, the UI goes a little away (don't mind the big blue magic circle in the middle, it's supposed to be there).
I don't think I have a problem with my Canvas settings (I make it so it's always facing the camera) but I don't see where else it could go wrong...
Thanks in advance for your answers !
Game Objects
Scene View
Correct UI
Wrong UI
Canvas Settings
The main reason for this is that the target exists in the world, while the slider exists on the canvas. Those two elements have different coordinate systems, and if you want the center of the Circle+Slider to be at the center of the Sphere, you have to project one set of coordinates to another one (World to Screen).
This can be done using the WorldToScreenPoint method on Camera.
Basically, the UI elements (existing on Canvas) will have to check the transform.position of the sphere in the world in Update function, and set their position using _mainCamera.WorldToScreenPoint(sphereTransform.position)
I have a square aim set to the middle of the screen, this is an UI image. Now I want to detect if enemies (visualized by pink boxes here) are within this aimbox but also, and this is very important, if only a part of the enemy is within the aimbox.
What I found so far is that I can't use (please correct me if I'm
wrong):
Raycast from midscreen, because then you have to aim directly at the
enemy.
Boxcast because it only creates the box at the target raycast point.
Raycast from the enemy to screen to detect object on screenpoint,
because the ray is cast from mid of the enemy.
Spherecast, because it's not a box and thus unreliable.
Using a box object (with collider) with a huge length into the
screen, because visually it will not retain the size of the aimbox
image.
I have tought about using several raycasts from the screen into the world using start points at 3,6,9,12 o'clock of the aimbox image position as well as from the cornors and midpoint of the UI aimbox image. But this seem like a really unreliable solution.
Here is an image of what I'm trying to achieve.
I need to detect all the boxes that are within, and are partial within, the aimbox UI image.
I have a 2D unity project.
I cannot depend on OnMouseExit because overlapping 2D box colliders cause the method to trigger even when the mouse is inside the bounds, since something else is in front (which is not my intention).
I was going to manually check for the mouse exiting on every frame by using:
if(!_collider.bounds.contains(Input.MousePosition))
But this does not work because `mouse position' is in terms of the number of pixels across the screen, and 'bounds' is in terms of "units" relative to the origin of the scene. The camera is Orthographic and slides around to look at the 2D plane that the world's sprites sit on. I have no idea how many "units" fit across the screen and suspect that it would change as soon as you change the aspect ratio or screen size.
You can use ScreenToWorldPoint(), to convert from screen point to 3D/2D point based on the camera's viewport, something like this:
if(!_collider.bounds.contains(Camera.main.ScreenToWorldPoint(Input.MousePosition)))
I have a question regarding the different sizes of the web player of unity.
I have a scene where the user will select the level he wants to play and i the camera can move only on up and down , not sideways , and the user can not zoom in or out.
My problem is that because of the different screen sizes the camera size will exit the bounds of the game and the user will see a black part of the scene.
What i need to do is to clamp the ortographicSize of the camera. On the left of the map and on the right i have 2 objects which are the borders.
How can i clamp the camera size in order for it to not go over the borders ?
Thanks
The camera.orthographicSize is the half size of the vertical volume (you can use the resolution of the horizontal volume to get the needed vertical volume).
Probably you are looking to implement this: sizeX/size = width/Height,
Use this to calculate SizeX.
what is the best way to position the Camera in a way that i can see what i paint in a certain region?
p.e. I'm painting a rectangle at around 300,400,2200. Where do i have to place the camera and which view do i have to set so that everything fits "in"?
Is there a trick or a special method or do i have to try it out with different camera positions?
There is no standard function that will position the camera this way because there are many options (think of different sides and rotations)
A trick you could use is:
Take the center of the MeshGeometry3D by using the Bounds property and add the normal vector several times to position the Camera.
Then use the normal vector of the plane, invert it and use it as the LookDirection for the camera.
How far you need to move the camera depends on the view angle of the camera. It can be calculated. Let me know if you want to know how (it will take me a little extra time)
More information can be found here too