- Home /
Make a game object disappear when your camera looks at it.
I'm creating a short horror game in which a mysterious creepy woman slowly follows you, but only when your not watching. when you turn around. I want to be able to see her disappearing. To create the "wtf omg thats creepy" effect. I would also be implementing random spawning as the level is a dark, danky skyscraper with about 7 floors. and the elevators broke so you must use the stairs. She is already following me. So i wanna add to the AI :)
Nah man i keep getting a parsing error on the first line.
I usually use js not too familiar with csharp.
Hi
I am trying to do something similar - I want the player to see an object in their peripheral vision and when they turn toward it - and object is now in center of the camera - the renderer is turned off and the object now spawns in another area of the screen. I would like this to be $$anonymous$$imally 3D but don't want to use triggers - just center of view. Thoughts?
@NDLP - Please open this as a new question. Don't ask questions as 'Answers' to existing questions. There is a simple approach to solve your problem.
Answer by hiddenspring81 · May 27, 2013 at 09:35 PM
To make an object disappear when they become visible, use the OnBecameVisible event. Create a new script, and add it to your "ghost" character in your scene. Here's some sample code to get you started,
public InvisibleWhenSeen : MonoBehaviour
{
const float ANIMATE_SPEED = 0.1f;
void OnBecameVisible ()
{
StopCoroutine("ToggleVisibility");
StartCoroutine("ToggleVisibility", true);
}
void OnBecameInvisible ()
{
StopCoroutine("ToggleVisibility");
StartCoroutine("ToggleVisibility", false);
}
IEnumerator ToggleVisibility (bool visible)
{
// Figure the desired alpha amount
float desiredAlpha = visible ? 1.0f : 0.0f;
// Find all the renderers attached to the object
var renderers = GetComponentsInChildren<Renderer>(true);
if (renderers.Length == 0)
yield break;
// Find out starting alpha amount
float startingAlpha = renderers[0].material.color.alpha;
// Track how long the animation has been playing
float totalTime = 0.0f;
// Loop until we reach the desired alpha amount
while (renderers[0].material.color.alpha != desiredAlpha)
{
// Update the alpha amount for each renderer
foreach (var renderer in renderers)
{
var color = renderer.material.color;
renderer.material.color = new Color(color.r, color.g, color.b, Mathf.Lerp(startingAlpha, desiredAlpha, totalTime * ANIMATE_SPEED));
}
// Pause momentarily, and then resume the animation
yield return null;
totalTime += Time.deltaTime;
}
}
}
The code hasn't been tested, but it should be enough to get you started.