Update an object when an other object is within camera view - unity3d

I am using a rig which is holding eye objects. My model is moving a lot later on, and for example the head will meet the ground, eyes down there as well.
Now there is a slight problem.
My object's origin is up in the rest pose head position.
If I let the entity move its head down and then zoom in on the eye until I am so close that the origin slips out of my view - the eye is gone, off on vacation. Only when I rotate my view to meet the origin, it reappears. This makes editing and observing difficult. Mind you, it's in Scene view and Game view where I observed this situation.
So I thought okay, I can check to update the object when offscreen. But that sounds highly unperformant on hindsight if I do that with each object. Now, there is an object, its main body, which is likely to stay in view more likely, origin wise. I wonder. Can I pick that other object's origin and determine the update cull with that? There is only a checkbox in my Unity version (2021.3.5f1), not a field to change that, and I am not really experienced in adding fields. So I googled a bit, but I couldn't find a solution, so I thought I ask here.
Edit: I tried this script, added it to the body and dropped in the eye renderer, which is sadly not working as much as I thought. Even though the main body is visible, the eye disappears in both views.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UpdateRendererOverrideManager : MonoBehaviour
{
public SkinnedMeshRenderer eye;
private void OnBecomeVisible()
{
eye.updateWhenOffscreen = true;
}
private void OnBecomeInvisible()
{
eye.updateWhenOffscreen = false;
}
}
Game view:

Related

How to adjust character height when playing seated in closed VR with MRTK?

I am creating a VR application with mrtk 2.1 and want to have the User on the same height when playing seated or standing.
When Playing seated the camera is too low.
I could adjust the height of the MixedRealityPlayspace which contains the MainCamera, but that resets after using the teleport function.
Additionally learned from this GitHub Post, that there is an Offset between the MainCamera and MixedRealityPlayspace which is not accessible via mrtk so calculating the right height would be difficult.
This just does not seem like the right approach.
How can I make sure that the User is always on the right head height in game even when playing seated?
I've gone ahead and filed the following feature request:
https://github.com/microsoft/MixedRealityToolkit-Unity/issues/6786
An untested workaround may be the following:
Create logic that instantiates a new GameObject at runtime.
Set the new GameObject's parent to be the MixedRealityPlayspace.
Set the new GameObject to be the parent of your main camera
Apply your desired offset as a transform to this new GameObject.
Talking with different MRTK Contributors, its felt that you may need to do this transform configuration at runtime. Trying to do this in the editor/in the scene hierarchy may encounter problems because folks were unsure whether or not the main camera's parent gets set as the MixedRealityPlayspace regardless of whether or not the main camera is a grandchild of the MixedRealityPlayspace.
OK i found a solution that works for me.
I use the following code to move the mixedRealityPlayspace up or down to achieve the wanted player height.(if you want to use this make sure to tag your mixedRealityPlayspace GameObject "Player")
var mixedRealityPlayspace = GameObject.FindGameObjectsWithTag("Player")[0].transform;
var camHeight = mainCamera.transform.position.y;
var adjustement =wantedPlayerHeight-camHeight ;
mixedRealityPlayspace.position= new Vector3(mixedRealityPlayspace.position.x,mixedRealityPlayspace.position.y+adjustement, mixedRealityPlayspace.position.z);
I call this on ones on the start of my Scene and in the OnTeleportCompleted Event described in this Dokumentation for the MRTK Teleport system

unity 2D (look at) object disappears

I have a moving object that I want to look at the target it's moving towards, here is my code:
void Update ()
{
transform.LookAt( target.transform.position);
}
The problem is the object does look at the target while moving, but it disappears from the screen, but it's there at the same :/
Edit: Since the game is in 2D view, the object kinda turns sideways, so when I was talking about it disappearing but still there, what is really happening is that it turns and faces the object it's looking at. I hope that makes things more clearer.
Check the sorting layer order of your 2d object. If it has an order less than the order of any other object then it will disappear behind it.
You can check it in detail from this link.
https://unity3d.com/learn/tutorials/modules/beginner/2d/sorting-layers

Two Cameras need them to face each other

I have two cameras in a scene,both following different objects I want to make them face each other ,like split screen but opposite sides,also I don't want to touch the rotation of the camera,any heads up with playing with cameras and split screening would be a great help,thanks!
This may be the thing you are looking for
// This complete script can be attached to a camera to make it
// continuously point at another object.
// The target variable shows up as a property in the inspector.
// Drag another object onto it to make the camera look at it.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform target;
void Update() {
// Rotate the camera every frame so it keeps looking at the target
transform.LookAt(target);
}
}
http://docs.unity3d.com/ScriptReference/Transform.LookAt.html
Found an easy way,created an empty gameObject rotated it upside down and inserted the second camera in it,still any other solutions are welcome and tips for multiplayer on same phone would also be great help,thanks!
Edit:This does not work.

How to change cameras in Unity based on camera coordinates?

I'd like to create a movie in Unity, so I would need several cameras and camerapaths.
On the top of this off course I'd like to change between them. For example if Camerapath1 reaches a significant point with Camera1 then I'd like to change to Camerapath2 with Camera2, etc.
I also have Camera Path Animator asset installed. It's working perfectly when I'm using it with only one camera for several camerapaths but I'm unable to change between maincameras.
I'm a newcomer to Unity. I also know that I should do something like this:
...
camera1.camera.active = false;
camera2.camera.active = true;
...
...but where should I populate these lines? On the top of this, how may I catch the event when a camera on a specific camerapath reaches a particular point?
The way to go would be an animation controller that has all camera as children and controls the active state of all cameras. This provides perfect control over the behaviour.
Create an empty game object, add all cameras as children, add an Animator to the main object with one animation. This animation takes all the camera and set their active state. One extra bonus of this approach is the possibility to call methods as well using the AnimationEvent process. You can still define within the animation for some triggered actions like explosions or movements of objects.
As I said, this give you perfect control since you can define easily actions at specific time.
The downside of it is the rigidity of the process. It may not be as flexible as code, but since you are making a movie, you probably do not need flexibility.
If so, you would have your cameras with a collider and rigidbody (isKinematic true), then you would have some trigger box with a simple script:
public void CameraTrigger:MonoBehaviour{
public GameObject nextCamera;
void OnTriggerEnter(Collider col){
if(col.gameObject.CompareTag("Camera")){
col.gameObject.SetActive(false);
nextCamera.SetActive(true);
}
}
}
Then you drag the camera meant to start next as nextCamera.

Unity3d 3d skybox not quite right

I'm messing around in unity3d in order to learn it.
Had a crack at making my own 3d skybox like in source engine for example. I'm using the standard 1st person controller.
I made another camera with same FOV for my skybox, and slaved it to the camera in the 1st person controller using the script below which I put on my skybox camera.
(Maincam field has the 1st person controller camera component in it)
using UnityEngine;
using System.Collections;
public class CameraSlave : MonoBehaviour {
public Component Maincam;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.rotation = Maincam.transform.rotation;
}
}
You can see result here. Its a bit funny. (The big tetrahedron shape in the background is in my skybox everything else is normal)
As far as I understand it as long as the camera fov is the same it doesnt matter what size my skybox things are.
I think the problem, is there is some lag maybe? Like the Update in the code above is being called one frame too late? I tried calling that update from the 1st person controllers mouse look script but as well as getting loads of errors the result was the same.
I can't visualize your example, btw:
I think the problem, is there is some lag maybe? Like the Update in
the code above is being called one frame too late? I tried calling
that update from the 1st person controllers mouse look script but as
well as getting loads of errors the result was the same.
You can't rely on the order in which Update method will be called by the engine (unless you force a particular order, but this isn't generally a good choice). For camera update operations it's better to use LateUpdate. It's guaranteed that it will be called after all Update methods.