我需要在Unity3D中找出一个点有多少光。

huangapple go评论56阅读模式
英文:

I need to find how much light is in a point in Unity3D

问题

I'm trying to create a video game in unity. In this video game, we have a thing named Mental Health. This decreases by 1 point per x seconds. The illumination of the surroundings of the player determines the seconds.

So, the problem is simple, I need to know how much light is in the player game object (all the time, like, in the Update method). I've tried using Light Probe groups, recommended by ChatGPT, but after reading the official documentation and failing repeatedly, I don't think it's the answer.

英文:

I'm trying to create a video game in unity. In this video game, we have a thing named Mental Health. This decreases by 1 point per x seconds. The illumination of the surroundings of the player determines the seconds.

So, the problem is simple, I need to know how much light is in the player game object (all the time, like, in the Update method). I've tried using Light Probe groups, recommended by ChatGPT, but after reading the official documentation and failing repeatedly, I don't think it's the answer.

答案1

得分: 0

Sounds like you need to calculate the player's proximity to light sources to determine how much the player's Mental Health should decrease. This would be easier than trying to measure light values, and provides some advantages (such as being able to detect baked and real-time lights).

首先,你可以确定什么算是“光源”。它是任何带有光组件的游戏对象吗?你是否对特定类型的光源感兴趣?这将帮助你确定需要检查玩家靠近的对象。

决定如何测量接近度。你想使用三维空间中的距离,还是其他方法?根据你的游戏设计,你可能还要考虑诸如视线或遮挡等因素。

一旦你确定了相关的游戏对象以及如何测量接近度,你可以编写代码来计算玩家与每个光源的接近度。一种方法是在Unity中使用Vector3.Distance方法,以找到玩家位置与每个光源位置之间的距离。

一旦你获得了距离,你可以使用它们来确定玩家的健康应该减少多少。你提到健康减少取决于附近光源的数量和光线的数量,因此也可以检查光线的范围/强度,然后决定如何将每个光源的距离和其范围/强度组合成一个确定x的单一值。

希望这有所帮助!😊

英文:

It sounds like you need to calculate the player's proximity to light sources to determine how much the player's Mental Health should decrease. This would be easier than trying to measure light values, and provides some advantages (such as being able to detect baked and real-time lights).

Firstly, you can determine what counts as a "light source". Is it any game object with a light component? Are there specific types of lights that you're interested in? This will help you identify what objects you need to check the player's proximity to.

Decide how you want to measure proximity. Do you want to use distance in 3D space, or something else? Depending on your game's design, you might also want to consider things like line-of-sight or occlusion🤷‍♀️

Once you've identified the relevant game objects and how you want to measure proximity, you can write the code to calculate the player's proximity to each light source😁 One way to do this is to use the Vector3.Distance method in Unity to find the distance between the player's position and the position of each light source.

Once you have the distances, you can use them to determine how much the player's health should decrease. You mentioned that the health decrease depends on the number of lights in proximity and the amount of light, so could also check the range/intensity of the light, and then decide how to combine the distances from each light source and their range/intensity into a single value that determines x.

I hope this helps!😊

答案2

得分: 0

以下是代码的翻译部分:

我自己试了一下,情不自禁!🤔;

如果我理解正确的话,请告诉我,这是你想要的:

使用 System.Collections;
使用 System.Collections.Generic;
使用 UnityEngine;

public class HealthController : MonoBehaviour
{
    public Light[] lights;
    public float maxDistance = 10.0f;
    public float healthDecreaseSpeed = 1.0f;
    public float minimumHealth = 0.0f;
    public float maximumHealth = 100.0f;
    private float currentHealth;

    void Start()
    {
        currentHealth = maximumHealth;
    }

    void Update()
    {
        float distanceSum = 0.0f;

        // 循环遍历数组中的所有灯光
        foreach (Light light in lights)
        {
            // 获取玩家到当前灯光的距离
            float distance = Vector3.Distance(transform.position, light.transform.position);

            // 将距离限制在 maxDistance 的最大值内
            distance = Mathf.Clamp(distance, 0.0f, maxDistance);

            // 将受限的距离添加到总距离和中
            distanceSum += distance;
        }

        // 计算玩家到所有灯光的平均距离
        float averageDistance = distanceSum / lights.Length;

        // 根据平均距离计算健康值减少速度
        float decreaseSpeed = Mathf.Lerp(0.0f, healthDecreaseSpeed, averageDistance / maxDistance);

        // 根据减少速度和上一帧之间的时间减少当前健康值
        currentHealth -= decreaseSpeed * Time.deltaTime;

        // 将当前健康值限制在最小值和最大值内
        currentHealth = Mathf.Clamp(currentHealth, minimumHealth, maximumHealth);

        // 将当前健康值输出到控制台
        Debug.Log("当前健康值: " + currentHealth);
    }
}
英文:

I gave it a go myself, couldn't help it!🤣

Let me know if I understood properly, and this is what you were after:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthController : MonoBehaviour
{
    public Light[] lights;
    public float maxDistance = 10.0f;
    public float healthDecreaseSpeed = 1.0f;
    public float minimumHealth = 0.0f;
    public float maximumHealth = 100.0f;
    private float currentHealth;

    void Start()
    {
        currentHealth = maximumHealth;
    }

    void Update()
    {
        float distanceSum = 0.0f;

        // Loop through all the lights in the array
        foreach (Light light in lights)
        {
            // Get the distance from the player to the current light
            float distance = Vector3.Distance(transform.position, light.transform.position);

            // Clamp the distance to a maximum of maxDistance
            distance = Mathf.Clamp(distance, 0.0f, maxDistance);

            // Add the clamped distance to the total distance sum
            distanceSum += distance;
        }

        // Calculate the average distance from the player to all the lights
        float averageDistance = distanceSum / lights.Length;

        // Calculate the health decrease speed based on the average distance
        float decreaseSpeed = Mathf.Lerp(0.0f, healthDecreaseSpeed, averageDistance / maxDistance);

        // Decrease the current health based on the decrease speed and the time since the last frame
        currentHealth -= decreaseSpeed * Time.deltaTime;

        // Clamp the current health to the minimum and maximum values
        currentHealth = Mathf.Clamp(currentHealth, minimumHealth, maximumHealth);

        // Output the current health to the console
        Debug.Log("Current Health: " + currentHealth);
    }
}

huangapple
  • 本文由 发表于 2023年2月26日 19:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定