Make sure the camera is in a specific direction

I am working to allow the character to click objects around. The problem is that as soon as he touches the object, he starts to move it, even when the touch was accidental and the character is not facing the direction of the object.

What I want to do is get the direction of the object when the collision occurs, and if the camara is actually facing that direction, the player can move it.

Right now I have managed to get the direction of the object, but I don't know how to compare this with the direction of the camera.

Here's what I'm doing now:

void OnCollisionEnter(Collision col) {
    float maxOffset = 1f;
    if (col.gameObject.name == "Sol") {
        // Calculate object direction
        Vector3 direction = (col.transform.position - transform.position).normalized;

        // Check the offset with the camera rotation (this doesn't work)
        Vector3 offset = direccion - Camera.main.transform.rotation.eulerAngles.normalized;

        if(offset.x + offset.y + offset.z < maxOffset) {
             // Move the object
        }
 }

      

+3


source to share


2 answers


You can try to achieve this in several ways. And it depends a little on how accurately you mean in front of the box.

You can receive events when an object is seen by a specific camera and when it enters or leaves using the following functions. With these commands from the moment the window is displayed with this camera (so even if only the edge is visible) your collision will cause.

OnWillRenderObject , Renderer.isVisible Renderer.OnBecameVisible , OnBecameInvisible

Or, you could try to figure out if the bounding box of the objects would fit inside the camera view frame, as you could use the following geometry commands.



GeometryUtility.CalculateFrustumPlanes , GeometryUtility.TestPlanesAABB

Or, if you have a very accurate downside, you can also go Physics.Raycast So you only fire an event when a ray hits an object.

Hope this helps you.

0


source


Take the obj compass and align it to ur device sorry, not when you move it, you can always find out what it is pointing at.



in theory this should work, but perhaps your object is just moving due to a bug in the engine of the simulator engine.

0


source







All Articles