0
\$\begingroup\$

I'm trying to control the intensity of the vignette depending on the distance to the player. The script itself is located on the prefab. When creating a prefab during the game, the vignette does not appear.

But if the prefab is added to the scene before the game starts, the vignette is visible and its intensity is adjustable. Prefab added before launch

The prefab is the opponent. Here are the vignette settings: enter image description here enter image description here

I will also add part of the enemy code where the vignette control should take place:

private PostProcessVolume postProcessVolume;
[SerializeField]
private Vignette vignette;

public float vignetteIntensityMultiplier = 1.2f;

private Transform player;

void Start()
{
    player = GameObject.FindGameObjectWithTag("Player").transform;
    postProcessVolume = GameObject.Find("PostProcessingVolume")?.GetComponent<PostProcessVolume>();

    if (postProcessVolume != null)
    {
        InitializeVignette();
    }
}

void InitializeVignette()
{
    if (postProcessVolume.profile.TryGetSettings(out vignette))
    {
        vignette.intensity.value = 0f;
    }
    else
    {
        Debug.LogError("Unable to get vignette settings");
    }
}

private void Update()
{
    float distanceToPlayer = Vector2.Distance(transform.position, player.position);

    if (distanceToPlayer > followDistance)
    {
        transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    }

    if (vignette != null)
    {
        float intensity = (followDistanceForVignette - distanceToPlayer) / followDistanceForVignette * vignetteIntensityMultiplier;
        vignette.intensity.value = intensity;
    }
}
\$\endgroup\$
11
  • \$\begingroup\$ Which object is "the" prefab? How have you configured this vignette? Please show us enough of your code and scene/inspector setup that a user could reproduce a Minimal Complete Verifiable Example of your problem starting from a new, empty project, without needing to ask any follow-up questions. \$\endgroup\$ Commented Jun 13, 2023 at 12:16
  • \$\begingroup\$ I have edited the post, please see if this is enough \$\endgroup\$ Commented Jun 13, 2023 at 12:38
  • \$\begingroup\$ From where are you setting/ getting the vignette in the Update? \$\endgroup\$ Commented Jun 13, 2023 at 13:09
  • \$\begingroup\$ I have variables like PostProcessVolume and Vignette, if that's what you mean \$\endgroup\$ Commented Jun 13, 2023 at 13:19
  • \$\begingroup\$ How is that variable assigned when the enemy is spawned mid-game as part of a prefab? When we say "complete" we mean we should be able to past your code samples into a new project without adding anything and they should compile and work and demonstrate the problem. If you've shared less than that, you're not done yet. \$\endgroup\$ Commented Jun 13, 2023 at 13:37

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.