How to make a playing area with fixed square dimensions? - unity3d

I want to have a playing area with a square dimension, and a sidebar GUI that can be resized according to the resolution. I drew a picture that might help explain
I tried following a tutorial here but the actual dimensions in the build I run seem to be different from what I configure it to in my editor. Also, how do I get the coordinates of the middle of the square (to instantiate something)? Any help? Thanks!

In the tutorial you linked it says:
When running your game from within Unity's editor, be sure to have the Game >window open and visible in the editor when you run the game. There's >currently a bug in Unity 3.0 (and possibly in earlier versions as well) where >the window resolution reported to the script does not match the actual >resolution of the window inside the editor if the window isn't visible at the >time the play button is pressed, leading to a viewport with the wrong size.
Did you took notice to this?
Also what do you mean by taking the coordinates in the center of the square? If you mean the actual screen, then you should get a point based on the square dimensions like:
Vector2 point;
Rect rect = camera.rect;
point.x = rect.x/2;
point.y = rect.y/2;
But if you want the point in 3D space where the camera is pointing, you can use the default method ViewportToWorldPoint, as shown here: http://answers.unity3d.com/questions/189731/how-would-i-find-a-point-in-front-of-my-cam.html
It should look like this:
float distanceFromViewPort = 1 //change 1 to the distance you wish the instance to appear away from the camera.
Vector3 point = camera.ViewportToWorldPoint(Vector(0.5,0.5,distanceFromCamera));

Related

How do I keep Canvas and non-Canvas objects close to each other?

I've got a board with checkers. The board is a mere container. The squares and the pieces are separate objects with a SpriteRenderer component and corresponding Square and Checker components.
I want to create an information window to the right of the board, containing players' names, their ratings, their time limits etc.
Lichess.com has a similar system:
The crux of the issue is there's some text in this window. I mean UI-Text. Thus, the window must be in a Canvas. However, if I make it a canvas, when changing screen aspect ratio, it moves onto or away from the board.
How do I "glue" the info window to the board?
Take var point = SpriteRenderer.bounds.max of the board (rop-right point)
Then use that point in var position = Camera.Main.WorldToScreenPoint(point) method to translate that position to the canvas
Set position to the panel you want to glue to the board
hint: to decide what part to glue to that point use RectTransofrm.pivot
example in the picture
Taking the canvas to world space doesn't solve the problem exactly?
Like the attached picture. => enter image description here

Unity UI screen size issue

I'm quite new to Unity, so I'm sorry if this is a basic question. I've been trying to set up the UI for a mobile game, but I'm not quite sure how to make the UI lock it's position, no matter the screen size. I've tried using anchors (though I don't fully understand how to use them properly), I've tried using a canvas scaler, I've looked at the Unity document and I just can't seem to find an answer. The buttons are off screen/half off the screen when I build the game to my device/switch screen sizes in the game view. Does anyone know how to fix this?
You can set your anchor point by selecting the UI object (such as a button) and then clicking here and selecting the right anchor point. You can also press down shift to set the pivot and/or alt to move the object to that point at the same time. The object should now be anchored to that point and keep its position even if the resolution is changed. You can set a precise position from the inspector, too. Simply adjust the Pos X and Pos Y variables. It will still adhere to the anchor point.
Note that you might have to play around with the Canvas object's UI Scale mode and its settings to get the right setup.

Unity 3D text disappears when I rotate the camera

New to Unity.
I think it's quite weird. What I was trying to do is to make the words on the newspaper flow like water. I created a plane for the newspaper, and I added a 3D Text object.
It worked well at first but when I rotate the camera(not the camera in the scene, but the camera with which you do all the editing), the text simply disappeared at some angles (particularly from below). The main camera had the same problem. It saw the text at some angle and couldn't at some other. I am sure that the texts are positioned right in the viewing frustum.
Some screenshots:
The text can be seem from here
And it cannot be seen when the camera moves a little bit down
Or from another angle, the text is selected
It is in the viewing frustum. But the main camera does not see it
It seems like your text and background are on the exact same coordinates (for example 1,1,1). By doing so, you let Unity decide which is more important to show on the foreground, which makes it dissapear sometimes.
Try to move your text a little forward (for example 1,1,1.001), so it seems to rest on, instead of in the newspaper. Most of the time, that should fix the problem.
Is there some kind of transparency on the object?
Try to set it to opaque to check if this is the problem

UnityVR - Having the both renders display the exact same image for each eye

When Unity builds a VR project, by default it is set to make the two views stereoscopic. It slightly offsets the camera position of one eye to give the user a sense of depth.
For example a square will appear slightly to the left on the right view compared to the left view.
I want to make the camera truly monoscopic by removing the offset that is created when i build the project. Each camera should render all objects in exactly the same position for both eyes.
One of things i tried was creating two camera and setting them to the left and right eye. Then i manually set the position/rotation of one camera until it looked monoscopic
It worked fine on my pixel phone, but as soon as i put the project on my test phone i noticed that the difference in resolutions messed up the view i was going for. The blocks were not in the same position when i looked at both renders.
If anyone has any solutions or ideas as to how i can go about this, i would greatly appreciate it.
Thank you!
You can still use 2 cameras, but instead of offsetting them, you can just make the width of the camera half.
Make 2 cameras, set their positions to exactly the same.
On the left eye camera, set the width to 0.5 and the x position to 0.
On the right eye camera, set the width to 0.5 and the x position to 0.5.
You should now have 2 cameras rendering the exact same thing, but twice across the screen, with no sense of depth.

Lower Left Coordinates of SKScene

I'm new to Swift SpriteKit programming and the coordinate system is driving me crazy. I create a sprite and I want to move it to the four corners of the screen. So, I set the position to (0,0). That's off the bottom left corner of the screen. Through some manual testing I've developed the chart below. The lower left and upper right are what the iOS simulator report when I touch the screen.
I have 2 questions:
1: Is there a method of determining the coordinates of the lower left hand corner of the view? Maybe I could build a dictionary with the coordinate values and the determine the machine type and then set the offsets. But, that's a lot of work and might not be accurate for new devices. It just seems that there should be a scene or frame property that I can use to put an object at the bottom left of the window.
2: The math doesn't work. In the iPhone5, 300 (lower left x) + 320 (width) = 620, not the reported 727. Same issue is true with the y coordinates. How does this work?
I set as few parameters as possible. I have not changed the anchorPoint or position of the scene.
Device Size LL UR
iPhone4s (320,480) (260,0) (766,764)
iPhone5 (320,568) (300,0) (727,764)
iPhone5s (320,568) (298,0) (727,764)
iPhone6 (375,667) (297,1) (728,765)
iPhone6plus (414,736) (298,0) (728,766)
iPad2 (768,1024) (226,0) (800,768)
iPad Air (768,1024) (224,0) (800,767)
iPad Retina (768,1024) (225,0) (800,768)
Ok, I think I figured this out. Setting scene!.scaleMode=SKSceneScaleMode.ResizeFill allow me to identify the four corners of the screen. So, now I can determine when a sprite crosses the edge of the screen. This doesn't seem to distort my images. I haven't been able to test it on a read device yet, but it leaves a blank area around the iPad2.
Applause for the hard work! haha
If I was going about getting values for the lower coordinates, I would use CGRectGetMinX to get the x-coordinate and CGRectGetMinY to get the the y-coordinate likewise:
CGPoint minimum = CGPointMake(CGRectGetMinX(self.frame),CGRectGetMinY(self.frame));
Then, if you wanted to get the top coordinates just use the same things but say MaxX or MaxY. Yeah, the coordinates are a bit confusing but if you use those then it will be a breeze.
EDIT: If you need to find if a body has exited outside visible space, so far what has worked for me is making a physics body to detect contact with it on the edge
[SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]
possibly another option you could try is to see the bounds of a UIScreen object.