I am working on using the Hololens Lens Toolkit Master.
The problem is that when you set the SetParent of the lens camera, the camera position of the lens becomes the same as the position of the parent.
For example, if A's position is 0, 0, 0 and B's position is 0, 0, 4, then A.SetParent(B.Transform) would make A's position be 0, 0, -4.
This is also true on Unity Editor.
However, if you build on hololens and run A.SetParent(B.Transform), the position of A will be 0, 0, 4.
I have no idea why this happens ...
I want 0, 0, -4 !!
Generally speaking the parent of the main Camera is the scene itself, you cannot go higher, so the parent is the camera, unless your camera is inside another game object. Also remember too, that with the Hololens, the camera is the stable position everything else needs to move in relation to the camera, not the other way around.
Update: So the main camera in hololens applications is like the character camera in a first person shooter, however its not the camera that moves its the world and the application is not in control of the main camera, in an FPS, you control the character either with a controller, or keyboard. The difference here is that the main camera is controlled by the hololens, 0,0,0,0 is the fixed main camera point and never changes, what happens is that the detection cameras in the hololens update position based on spatial mapping routines in the device, so changes to the position of the main camera will have unexpected results. If you want to change the point of view, I would recommend that you create a new camera and transfer the view to the new camera and move that camera, and the revert back to the main camera when you want to switch it back. This new camera cannot be a child of the main camera. One caveat is that I have never tried this, and is offered as a possible solution, I do not know if it will work or not.
There are two overloads for the SetParent function:
SetParent(Transform parent)
SetParent(Transform parent, bool worldPositionStays);
The first one uses true for the parameter as default. Use the second function and pass false to it to force the object use its location position when setting its parent.
A.SetParent(B.Transform, false);
Related
I'm having trouble setting an image behind a gameobject in unity. I saw a lot of similar questions but none solved my problem. So basically I need to have the image as a child of the object to be able to do what I'm to do. I added an image of my unity view.
You can likely cheat this by using multiple cameras and layers. Set your container GameObject to its one layer (say "GameObjectLayer",) and set your image's GameObject to another layer (say "ImageUILayer".)
Then, create two cameras, and set the following properties:
Camera 1:
Depth = 0
Culling Mask = "GameObjectLayer"
Clear Flags & Background = (whatever you want the main camera background to be)
Camera 2:
Depth = 1
Culling Mask = "ImageUILayer"
Clear Flags = Depth only (this keeps the camera from drawing a background at all)
Each camera renders in turn, starting with the lowest depth and working up from there. Thus, Camera 1 renders everything in the "GameObjectLayer" first; then, Camera 2 renders everything in the "ImageUILayer" over top of that.
It's an extremely useful workaround when you're trying to mix 2D sprites with 3D objects, and it will never give you any flicker or other z-positioning weirdness, because the cameras each render all their layers in turn.
You can use more than two cameras if you need more than two layers of 3D/2D objects; I'm currently using a total of seven cameras with no ill effects.
in your case I suggest you use the(right click and choose)>2D > Sprites and put the sprites behind of the Gameobject
I spawn the prefab in a gameobject and that gameobject is a child of UI Image what I'm trying to accomplish is display the prefab infront of a UI Image. I've tried making z position of gameobject to negative but nothing happens. I can't place the sorting layer of UI Image cause it doesn't exist. What should I do? Is this even possible?
Update
Tried adding second camera. I made gameobject layer to SecondCam. Change settings of second camera to depth only and it's culling mask to SecondCam only then it's layer to SecondCam also. Change settings of main camera culling mask to everything except SecondCam. But this doesn't work, Have I done something wrong?
Second Camera settings
Main Camera settings
If your Gameobject is not one of the UI components you may need to give it another layer and render it with another camera with depth property higher than the UI camera.
You can change the sorting layer on prefabs or any object created on runtime.
If your UI canvas is set to screen overlay, then it with ALWAYS render on top. You have to use a screen space or world space UI, along with Dave's answer.
I'm developing for oculus go, and I'm making a sky dive like game where the player controls the hovering of the character with the head. I created an empty game object and placed the ovr camera rig inside to move it, then i use camera.main.transform.rotation to get the oculus rotation, and to move the character to the direction the player is looking at. For example, i print the camera.main.transform.rotation and get 4 values from -1 going to 0 and finally 1, what i did to get the tracking was
if(camera.main.transform.rotation.x>0)
{
//move the player up
}
Imagine if the oculus go prints values from 0 to 1 when I look up with camera.main.transform.rotation.x
When I get the apk it's fine and all but sometimes the axis are inverted and I have to restart the oculus go to fix this. Does anyone know why this happends, and what I'm doing wrong?
I had a similar problem with the z-axis controls. I'm doing a racing game for the Oculus go and wanted to control the direction of the car with the LocalControllerRotation.
In fact, the inversion happens when you rotate the oculus more than 180 ° in global space
(0° = LocalControllerRotation.w = 1f, 180° = LocalControllerRotation.w = -1f)
The problem was that the global value of Oculus did not change even after the view was reset. The Oculus scripts reoriented all controllers and cameras, but the values did not. And because my script worked by accessing the values, the result was reversed.
My solution was to check the LocalControllerRotation.w value (should also work for the camera orientation) and then change the input
I have two cameras,
why I have two cameras?
because I want some game object in front of the Canvas so that it could move around, maybe could have some particles if it feels happy etc.. So:
I set up the main Camera with mask option UI and make it in charge of all the UI stuff, and to make sure it does not overlap everything I set the canvas mode "Screen-Space Camera"
I set up another camera and make it capture everything else except the UI stuff.
set their depth so that the main Camera with UI will behind the other Camera.
things go well now, I could see the game object before the UI stuff. Cheers!
But in some case I what to convert the UI element to the point in the world space, So that I could generate some gameObject near the UI element and maybe Tween it to move to another UI Element, let's take collecting Gems as an example.
Usually, I could just do that with following code:
GameObject go = Instantiate(eff_gem);
go.transform.position = Camera.main.ScreenToWorldPoint(p1.GetComponent<RectTransform>().position);
go.transform.DoMove(p2.GetComponent<RectTransform>().position, 1f).OnComplete(()=>{
Destory(go, 1f);
});
The main idea is to instantiate a gem near a button and make it fly to the count panel.
But since I make the canvas "Screen Space Camera" instead of "Screen Space Overlay", all the above code will be a mess. furthermore, I think I have to convert the screen point of the canvas element to the world space by the other camera, But I can't even get the actual pixel position of the canvas element by
p1.GetComponent<RectTransform>().position now.
So How Can I do that?
I have made a demo project to demonstrate the issue.
var p1R = p1.GetComponent<RectTransform>();
Vector3 p1WorldPos = p1R.TransformPoint(p1R.rect.center);
//use p1WorldPos in your other camera
I have two camera with "MainCamera" tag in a scene.
using this line of code
camera.main.transform.name
I am getting camera name but the problem is, it is only showing one camera name (which is fine) No matter what is depth or order of both main cameras in hierarchy.
How to get only second camera instead the one it is showing!
I only want to use camera.main property and nothing else option.
Camera is nothing but a GameObject with a Camera component attached. So it is very simple to get all instances of the camera:
var cameras = FindObjectsOfType(typeof(Camera));
foreach (Camera camera in cameras)
Debug.Log (camera.name);
In case you want to access the camera inside a rendering method (OnPreRender, OnPostRender, ...), you can access the camera currently rendering via Camera.current.
Other than that, there is no way to get a "second main" camera. Camera.main, will always return the first enabled main camera. You have to go with David's solution.