I have the following setup right now:
"Startup" scene which initializes all managers (spatial mapping, cursors, camera, ...)
"Main" scene with animations and one object which moves around
Once the users interacts with the program the scene restarts at some point, then the following should happen:
Object's coordinates get stored (E.g. DontDestroyOnLoad() on a "storage object". I need to destroy the object since it needs to reset itself on scene restart)
Scene restarts
Object gets recreated with stored coordinates
Code:
DataPositionStorer positionStorer = new GameObject("DataPositionStorer").AddComponent<DataPositionStorer>();
DontDestroyOnLoad(positionStorer.gameObject);
// dataPoints is the specific object I need to keep the coordinates of
positionStorer.Position = dataPoints.transform.position;
positionStorer.Rotation = dataPoints.transform.rotation;
positionStorer.Zoom = dataPoints.transform.localScale;
// Reload current scene ("Main")
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
And in the startup method of the object:
DataPositionStorer positionStorer = FindObjectOfType<DataPositionStorer>();
if (positionStorer!= null)
{
dataPoints.transform.position = positionStorer.Position;
dataPoints.transform.localRotation = positionStorer.Rotation;
dataPoints.transform.localScale = positionStorer.Zoom;
}
This setup works in the unity player but once I run it in the hololens emulator / on a hololens the position afterwards isn't correct anymore.
Can somebody help me resolve this problem on the emulator / hololens?
Thanks to the two guys commenting I've found a solution:
Load the WorldAnchorManager Script in the Setup scene and make it DontDestroyOnLoad
Attach a World Anchor before the scene restarts
Load the World Anchor after the scene restarted
This however only works on the emulator / HoloLens itself. In the Unity Editor World Anchors don't work! So just copy paste the coordinates over like I did in my initial post.
Related
Trying to move a player object to the start location built into each level of the game at level start but the object does not reposition. The player is not a child of another so I am working with root transform here.
//move player to start position (I know find is expensive)
startPosition_GO = GameObject.Find("StartPosition");
playerGO.transform.position = startPosition_GO.transform.position;
//and just to be because
playerGO.transform.position = new Vector3(startPosition_GO.transform.position.x, startPosition_GO.transform.position.y, startPosition_GO.transform.position.z);
player object stays where it was and does not move.
including two images, first before starting the level you can see the starting location game object (the pink square). The code above is called at level start. The second image shows where the player (with the arrow nav icon on it sits after the code is executed.
Try just this code to move the player position.
startPosition_GO = GameObject.Find("StartPosition");
playerGO.transform.position = startPosition_GO.transform.position;
Set the position once. You could remove this
playerGO.transform.position = new Vector3(startPosition_GO.transform.position.x, startPosition_GO.transform.position.y, startPosition_GO.transform.position.z);
Or this and it should work.
playerGO.transform.position = startPosition_GO.transform.position;
The code seems redundant which could lead to issues.
I have two cameras in a scene and I want to switch between them without disabling them. Is it possible? If it is then can you show how it is done? Thanks
A nice way to use different cameras and manage transitions is using the Cinemachine package made by Unity. In the following animation I am switching between two Virtual Cinemachine Cameras by changing their priority.
Create a new empty scene with the default camera.
Add a sphere to the center of the scene. This will be the object we want to look at with our cameras. Maybe also add a plane below the sphere like in the image above, for orientation.
Go to Window->Package Manager. Search for Cinemachine and install it.
Select the current Main Camera gameobject in your scene and add a CinemachineBrain component.
There is a new main menu entry called Cinemachine. Select "Create virtual camera" two times. Two gameobjects with a "Cinemachine Virtual Camera" Script will be added. The CinemachineBrain component acts as a coordinator for those Virtual Cameras.
Position the two Virtual Cameras in your scene and drag the Sphere into the "Look At" property of both Virtual Cameras.
If you start the project, the first Virtual Camera will be active. You can raise the "Priority" property of the second Virtual Camera and the view will transition to the second camera. Alternatively you can disable the first Virtual Camera.
Cinemachine is very powerful. You can change the transitions between Virtual Cameras in the CinemachineBrain component. Select a different "Default Blend" or add "Custom Blends" for transitions.
View some Official Cinemachine Tutorials to unleash their full power.
A really easy way is to disabling just the Camera Component.
I had a scene with 6 cameras with different post processing , one of my AA plugins was making game crash after disabling camera object , then I tried to only disabling Camera Component.
Now it works just fine :)
the second thought is just delete and adding Camera Component to camera objects using an script.
How about changing the Tag on the cameras. Usually the mainCam has the MainCamera tag.
I am not sure if that works tho..
I made a scene that represents the main menu of a game. In this menu there is an option for single player, and, when I press it, it opens a new "level 1" scene.
Also, there is a pause menu that allows the player to quit the game and get back to the main menu.
However, after it backs to the main menu, when I try to get into the level 1 scene using SceneManager.LoadScene("level 1"), it makes a lot of problems. It means that a lot of game objects don't act like they should.
I dont know why it happened, and I would appreciate if someone can help me solve it. Thank you.
You can use SceneManager.LoadScene("level 1", LoadSceneMode.Single); to clear the old scene and only load the new one in, which will let you get a clear scene and shouldn't have any problems.
If you have scripts that need to last into the new scene, you can use DontDestroyOnLoad(this); in your scripts and they will move into the new scene. The problem is you have to make sure your scripts that aren't being destroyed don't try and access destroyed objects. From your comment it looks like you have a player movement controller that isn't being destroyed, or it's being created in the menu scene and it doesn't have it's references updated correctly when the scene is reloaded. You can also use Destroy(this); in your scripts that are saved through scene changes to get rid of them when they're done running.
I am a beginner with Unity3D.
Until now I have learned a few things.
For example, create a simple 2D menu, switch from one scene to another with an appropriate C# script and associate the scripts with the objects.
The problem I pose here is about the behavior (strange to me) of the white panel behind the menu when I change the scene.
I have two scenes, the first which is the menu and a second one is the actual game.
I used UI elements to create the first one, while I used 3D objects to make the second one.
So, what happens?
It happens that when you step into the second scene by clicking on the green button the background becomes brown. If, on the other hand, I run the second scene individually without going through the first one (launching the scene directly), the background remains white.
See this short video for further details.
Why?
Listed below are some images, hoping they can help me understand my problem.
The first scene (dev mode):
The first scene (running):
The second scene (dev mode):
The second scene (running directly):
The second scene (running after clicking the green button "Comincia"):
The script I use to change the scene (linked to the green button):
using UnityEngine.SceneManagement;
using UnityEngine;
public class GameScene : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PlayNow () {
SceneManager.LoadScene("Game", LoadSceneMode.Single);
}
}
Update: I have updated the information on the application. I added a new screenshot where you can see the camera settings. Meanwhile, I changed some things, but the problem still remains. I also noticed that this problem only happens when I switch to the Game scene, when I switch to other scenes instead it does not.
Added a new screenshot showing the inspector of the Main Camera object:
I was able to understand where the problem was: I made an improper use of the Plane object (highlighted in the screenshot). By removing it, now it shows correctly the solid color white background as set on the Main Camera.
I am creating AR app in which I have 10 Image targets( only 1 recognition at one time). I want to keep the 3D object to be retained even after Target image is lost and update the 3D model after it again found image target (same / or different target)...
I have done so many things like parent change,coroutine, Invoke but none seems to work..
Thanks in advance!
You can prevent the object from disappearing when target is lost. Simply change the DefaultTrackableEventHandler in the (EDIT) OnTrackingLost method to do nothing.
That will result in your model hanging in the middle of the screen if you lose it fast enough. Of you move away from the target slowly, model gets to the edge and it may be that the tracking gets lost while out of screen.
EDIT:
void OnTrackingFound(){
TrackerObject[] trackers = FindObjectsOfType<TrackerObject>()
foreach(var t in trackers){ t.SetOff();}
this.gameObject.GetComponent<TrackerObject>().SetOn();
}
then you have this TrackerObject component that is attached to all ImageTarget. It has SetOn/Off methods to do what it says.
You can make the code better with storing the info instead of looking for them each time.
Just enable Extended Tracking from Inspector menu .
For Example :
You can take the position and orientation of the detected object when Vuforia Target is there and then you can Instantiate the same object as:
GameObject generatedObj = Instantiate(mTrackableBehaviour.gameObject, new Vector3(0,-4.0f,18), Quaternion.identity);
generatedObj.transform.localScale = new Vector3(1,1,1); // change its local scale in x y z format
generatedObj.AddComponent<Translate>();
After doing so disable the ImageTarget from scene and enable again when you want it to be depedent on Marker.
But this has the limitation that
World co-ordinate experience will not be experienced,it will be like
https://www.youtube.com/watch?v=iHhMCdh3k7U
Check if you donĀ“t have enable the option "Track Device Pose" in Vuforia Engine Configuration in ARCamera object, this option cause object keep in screen.
Disable "Track Device Pose" and this resolve the issue.
Unity 2018.3.6 With Vuforia 7