Logic to detect objects entering between the player and camera?

Hello,

I have a Camera following a player and at times, Object enter between the player and the camera. Now more 75% of the time, the camera and the player are separated exactly by 40 units.

Target : iPad

Game View : X-Z i.e. the player falls down due to gravity and camera follows the player.

Now the objects which enter have collider and some do not. To make things simple I am only considering the one with a collider. They do not have rigidbody.

Function Setup :
I have script upon the parent of the gameobject that enter the space between the player and the camera. That script has two public functions which are responsible for transparency ON and OFF. Now these functions can just be called once to perform it’s respective tasks.

Problem:

  • I have to call them when it enter the space and also exits the space between the camera and the player.
  • As these object are plenty in number & my target is iPad I can’t use rigid bodies.

POSSIBLE SOLUTIONS:

  • Planned to use colliders but the thing is I have to use Rigid Bodies in order to use them.
  • Raycasting/Spherecasting etc could be used, but how do I revert the transparency when the GObject exist the intermidiate space.

Thanks Devs

regards,

Karsnen.

So you could call your transparent ON/OFF methods based on this for closer to the camera

#Player.cs#

 public static float distanceSquared;
void Update()
{
     //note you should cache transform and Camera.main.transform for performance
     distanceSquared = (transform.position - Camera.main.transform.position).sqrMagnitude;
}

#Thing.cs#

  void Update()
  {
   if((transform.position - Camera.main.transform.position).sqrMagnitude < Player.distanceSquared)
   {
        //Make it transparent: e.g.
        renderer.enabled = false;
        //or
        var c = renderer.material.color;
        c.a = 0.3f;
        renderer.material.color = c; //presuming that material is transparent in some way
   }
   else 
   {
        //Make it opaque: e.g.
        renderer.enabled = true;
        //or
        var c = renderer.material.color;
        c.a = 1f;
        renderer.material.color = c; //presuming that material is transparent in some way
   }
  }