- Home /
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 were you thinking that you want the transparency ON when the object is anywhere between the player and the camera or only if it would occlude some part/all of the player's avatar?
BTW if it's moving it and it has a collider you should attach a kinematic rigidbody even if you don't want physics. The cost is high otherwise.
Whydoidoit :
First part of your question : I want the transperancy ON when the object is "anywhere" near the player & camera. The purpose is to give the user more view of the environment. I hope you get what I mean here. The basic notion is that, I do not what those gObjects to obstruct the view and clog the screen space.
Second part of your question : I am not sure what you meant when you said occlude part/all of the player's avatar.
I mean - 1) if the distance between the camera and the object is less than the distance between the player and the camera it should be transparent?
Versus - 2) it should be transparent if the player cannot be seen because it is in the way?
1 is mathematics, 2 is raycasting...
$$anonymous$$Ike, Thanks for your reply. I was thinking about $$anonymous$$athematics only. But I am going to have a lot of such objects in the environment and there are going to be a lot of update functions. So considering my target as iPad, is that expensive?
Similarly on the other hand, if I happen to use Raycasting - how do I revert the transparency when it has to be visible as soon as it exits the raycasting? Could you pl
Answer by whydoidoit · Nov 22, 2012 at 09:52 PM
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
}
}
Regarding changing the transparency, I do not have a transparent shader upon it. Hence first I switch the shader to a transparent one and then lower the alpha value. Then when I want to revert it back to the original one, I revert it back to $$anonymous$$obile/Bumped Diffuse. Is that okay?
Thank you $$anonymous$$ike. I am going try the above code.