Canvas scaling for GameObjects in Unity - c#

I have searched online for the problem, and they always result in code. I am trying to do scaling of GameObjects just like I would an Image. Let me explain, here is an Image of me manipulating an image with a canvas:
Now here is an image of my paddle in inspector
The paddle has an image, a ridged body, box collider, etc. But I want the paddle to be able to be scaled like the background image gets scaled. Is there a way I can put the paddle in the canvas so that the box collider, image, etc scales. Any help with this is extremely appreciated.

No, I don't hink there is a simple solution to your problem, because the elements you want don't necessarily have a width and height that means something in a RectTransform.
For instance, your box collider is a 3D shape (speaking of this, you might want to use a BoxCollider2D)
Anyway, Only the Unity UI components react to RectTransforms. Colliders aren't UI elements. All the components you talk about react differently:
Image is a 2d ui component with a width and height. It will scale in your canevas
BoxCollider isn't a UI element. it won't scale, but you can do it programmatically by copying the values of transform.Rect inside collider.Size. I am pretty sure you can find several examples of that on the internet
Paddle is your own component, so of course it will only do what you programmed it to do. I don't see what you mean by "put the paddle in the canvas". Your paddle isn't a graphic element. It doesn't have a width and height. The Image component however will have a width since it is a UI component

Related

Unity - How to rotate game screen?(Not mobile phone rotation)

I'm using Unity 2020.1.17f.
To do align correction between VR lens and Display, I want to control size of screen, offset of screen, and rotation of screen(screen means area of display where actual game scene is appeared, my game has letter box in 4 ways).
I could control size and offset by changing values of Viewport Rect, but I cannot find a way to control rotation of screen. (Like Cv2.GetRotationMatrix2D() of openCV in C#)
You can say "why don't you rotate your camera?", but rotating camera is not what I want. As you know, in VR, Game screen is distorted in several shapes to optimize to their lens shape. Thus, even I rotate camera, it is not helpful for mis-align correction.
In Google, there are many answers, but only related with rotation of mobile phone, not controlling rotation in arbitrary angle(that what i really want).
How can control of rotation of game screen?
You need to go to Edit/PlayerSettings/Player
And open the section called resolution and presentation
as you see there is a section called default orientation and here you can set the orientation of the build just opened.
Down there is also a section called Allowed orientations for auto rotations where you can set the other rotations allowed. In my case, everything is allowed, but you can just set the 2 landscapes or the 2 portraits depending on your game and your canvas and camera orientation

Unity UI slider doesn't work when placed under another game object

I'm making a main menu for my Android game and I ran into this bug I suppose which I can't seem to resolve.
I made the options button reveal a rect transform that shows sliders for volume control and buttons for right or left shooting button placement.
The UI buttons seem to work fine, but the sliders won't slide. If I place the sliders outside of the game object with the rect transform mask it works, and if for example during the editor play test I copy the slider inside the game object the copy also works, but then for example if I take a slider that was outside of the game object and put it under the game object during the play test it again won't work.
Has any of you encountered this issue? How can you work around that so I maintain the revealing-window effect? Thanks.
EDIT: I fixed it by resetting the scale values of the rect transform of the holding game object to 1. It was set to 117 while the width and height were set to values 117 times smaller than they would have otherwise. I don't know why it messes with hoe the touch is read for the sliders, but it was resolved after scaling it down to 1 and adjusting the rect width and height and the UI objects' scale back.

Detect UNITY UI image only moves inside another image or rect transform

I have two image in my unity scene.
One of this images is an square as background Recttransform and another image is a button.
I use Drag Event for this button to detect movement in run time.
But I want to move this button only inside the background image.
And I don't know how to make this limitation.
Is there any solution for this issue ?
For more explanation please see this image :
Off the top of my head, here are a two ways to do this:
Create a few invisible box colliders and place them at the edge of the red square. Probably easiest but more prone to issues when screen size changes.
Get Screen.width, Screen.height, Rect.width, and Rect.height. Only allow drag up to the edges of the screen minus 1/2 Rect.width and 1/2 Rect.height.

ScrollRect not scrolling when canvas Render Mode is set to Screen Space - Camera

Did you find out why this was happening? I'm having the same issue here.
I had a canvas with a scroll bar working normally, I needed to set the canvas render mode to "Screen Space - Camera", and now the bar doesn't move anymore.
When you change the Canva's Render Mode from Screen Space - Overlay to Screen Space - Camera, an new slot called Render Camera will appear under the Canvas. Drag your main camera into this slot. That camera will be used to send events to the UI elements.
If you've already done this but the problem is still there, delete the ScrollRect and create a new one. Finally, if the ScrollRect is still not moving, make sure that no other UI object is blocking it.
Maybe a hidden panel or image with 0 alpha... Any UI object that is below the ScrollRect in the Hierarchy tab will block the ScrollRect if on top of it.
EDIT:
Once you drag the main camera to the Render Camera slot, Plane Distance slot will appear. Also make sure that it's not set to 0. Mine is set to 100 as shown in the screenshot above and that should work fine.
There might be multiple reasons why your scrollview doesn't work
I encountered with a similar problem. My canvas had a background image so i had to put a Image element without a sprite and set the alpha to "0" within Content of scrollview.
when your viewport field is not referring to viewport object in scroll rect
Try altering the movement type
I also had this problem due to not having an EventSystem in the scene.

VR score text display

I am trying to make a VR game with google cardboard in unity. However we can not find a way to display score text right in front of the player. However when I add 2D text it is only on one side and therefore on one side of the eye and getting the position right for 2 texts is hard. If I use 3D text and set in front of the players position I think it will go into the wall if a player hits one. Is their any way to display a score on google cardboard / Unity VR.
You can either use native Unity Canvas UI or Googles hack to render OnGUI calls onto a texture.
I would definately recommend Canvas as that is the way Unity is working on their UI features, and it has much better layout capability.
To use canvas, Right click in the hierarchy and add UI->Text. You will automatically get a canvas. The important part is set the canvas to world-space (not screen space overlay). Then drag the canvas game object so it is a child of the Google Cardboard Main head object. Scale it down (like x:0.001,y:0.001,z=0.001) because by default it will be massive. To avoid going through walls position it about 0.5m in front of the camera - within any collider you may have.
there is another approach as well which you can place that canvas under camera make it world space, and then adjust it as you want, after that where ever you look at it will be seen easily ( as suggested by earlier user in answers) as you can see below in picture i placed canvas > Text > under camera which i used for oculus/ google camera

Categories