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 :slight_smile:

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.