You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中如何根据分数持续调整生成预制体的缩放比例?

更新说明

此前代码存在混淆,已做修改。玩家与生成物体碰撞时,物体会被销毁,玩家得1分。累计获得一定分数(本例为3分)时,后续生成的物体需开始缩小。

核心目标

玩家每获得3分,生成的物体缩放比例缩小0.1,该过程可重复触发。

当前实现逻辑

游戏管理器(Game Manager)

负责分数追踪的代码:

//Singleton
public static GameManager gameManager { get; private set; }

//if repeat score = 4 reset to 0 to repeat the process  
public int repeatScoreGoal = 4;

[HideInInspector]
public int repeatScore = 0;

//access this method in another script to increase repeatScore value
//when they player collides and destroys object
public void RepeatScore()
{
    repeatScore += 1;

    if (repeatScore >= repeatScoreGoal)
    {
        repeatScore = 0;
    }
}

碰撞检测与分数增加

玩家脚本中碰撞物体时增加分数的方法:

private const string tagDestructible = "Destructible";

private void OnCollisionEnter(Collision other)
{
    if (other.gameObject.tag == tagDestructible)
    {
        if(other.gameObject.tag == tagDestructible)
        {
            GameManager.gameManager.RepeatScore();
        }
        
        Destroy(other.gameObject);
    }
}

预制体缩放脚本(挂载于预制体)

未在ObjectSpawner脚本中处理缩放,而是给预制体单独挂载脚本,让每个预制体拥有独立缩放比例,方便后续为不同预制体设置独特缩放值:

using UnityEngine;

public class PrefabScale : MonoBehaviour
{
    
    public int recScore = 3;
    private int currentScore;

    private bool objectScale = false;

void Update()
{
    //variable referencing to singleton
    currentScore = GameManager.gameManager.repeatScore;
    
    //if reapeatSore in Game Manager = recScore, scale object
    if (currentScore >= recScore && objectScale == false)
    {
        objectScale = true;

        if (objectScale == true)
        {
            gameObject.transform.localScale += new Vector3(-0.1f, -0.1f, 
            -0.1f);
        }

        if (currentScore == 0)
        {
            objectScale = false;
        }
    }
}

物体生成脚本(挂载于生成预制体的空物体)

用于生成预制体的ObjectSpawn脚本:

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

public class ObjectSpawn : MonoBehaviour
{
    [SerializeField]
    float[] percentages;

    public GameObject[] prefab;

    public float spawnRate = 1f;
    public float minHeight = -1f;
    public float maxHeight = 1f;

    public int recScore = 3;
    private int currentScore;
    
    //ignore these variables, view notes in update method for more info
    /*
    private bool spawnPointPosition = false;
    private float resetSpawnPoint;
    */

private void OnEnable()
{
    InvokeRepeating(nameof(Spawn), spawnRate, spawnRate);
}

private void OnDisable()
{
    CancelInvoke(nameof(Spawn));
}

private void Spawn()
{
    GameObject objects = Instantiate(prefab[GetRandomSpawn()], 
    transform.position,
    Quaternion.identity);
    objects.transform.position += Vector3.up * Random.Range(minHeight, 
    maxHeight);
    objects.transform.SetParent(transform);
}

private int GetRandomSpawn()
{
    float random = Random.Range(0f, 1f);
    float numForAdding = 0;
    float total = 0;
    for (int i = 0; i < percentages.Length; i++)
    {
        total += percentages[i];
    }

    for (int i = 0; i < prefab.Length; i++)
    {
        if (percentages[i] / total + numForAdding >= random)
        {
            return i;
        }
        else
        {
            numForAdding += percentages[i] / total;
        }
    }
    return 0;
}

private void Update()
{
    /*I left this singleton reference here just incase we need it for 
      the current problem we are trying to sovle */
    currentScore = GameManager.gameManager.repeatScore;
    
    

    /*ignore this below, this is adjusting the x position of the 
      empty game object that is spawning the prefabs and does not relate 
      to the scaling of the prefabs*/
    /*

    resetSpawnPoint = GameManager.gameManager.score;

    if (currentScore >= recScore && spawnPointPosition == false)
    {
        spawnPointPosition = true;

        if (spawnPointPosition == true)
        {
            gameObject.transform.position += new Vector3(0f, 0f, 0f);
        }

    }

    if (currentScore == 0)
    {
        spawnPointPosition = false;
    }

    if (resetSpawnPoint == 0)
    {
        gameObject.transform.position = new Vector3(20f, 0f, 0f);
    }
    */
}
当前问题

目前实现中,玩家达到目标分数(3分)时,已生成的物体会缩小,但后续新生成的物体仍为预制体原始大小,直到玩家再次达到目标分数。需要实现:每次玩家获得3分,现有物体缩小0.1,且后续新生成的物体也保持该缩放比例,过程可重复执行。

当前现象

  • 游戏启动时物体缩放为1;
  • 玩家销毁3个物体获得3分后,屏幕上的物体缩放变为0.9,但后续生成的物体恢复为1。

期望效果

游戏启动后,玩家获得3分,现有物体缩放为0.9,新生成的物体也为0.9;玩家再获得3分,现有物体缩放为0.8,新生成的物体也为0.8,以此类推循环执行。

内容的提问来源于stack exchange,提问作者Mr. Milieu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 16:07:01