c# - 我无法随着时间的推移(每秒)提高预制件的速度(Unity)
问题描述
浮动会随着时间的推移而增加,但它不会保持在下一个产生的那个上,所以前 Meteor 1 = -2.5 速度,当它被摧毁,然后 Meteor 2 = -2.5,当它产生时,但我的游戏让它们都在 -2 产生速度 这是移动器本身的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeteorMover : MonoBehaviour
{
public float speed = -2f;
Rigidbody2D rigidBody;
// Start is called before the first frame update
IEnumerator Start()
{
rigidBody = GetComponent<Rigidbody2D>();
// Give meteor an initial downward velocity
rigidBody.velocity = new Vector2(0, speed);
WaitForSeconds wait = new WaitForSeconds(1);
int time = 1;
while (time > 0)
{
speed -= .1f;
time++;
//yield return speed;
yield return wait;
}
//yield return speed;
yield return wait;
}
}
然后是 spawners 脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeteorSpawn : MonoBehaviour
{
public GameObject meteorPrefab;
public float minSpawnDelay = 1f;
public float maxSpawnDelay = 3f;
public float spawnXLimit = 6f;
// Start is called before the first frame update
void Start()
{
Spawn();
}
void Spawn()
{
// Create a meteor at a random x position
float random = Random.Range(-spawnXLimit, spawnXLimit);
Vector3 spawnPos = transform.position + new Vector3(random, 0f, 0f);
Instantiate(meteorPrefab, spawnPos, Quaternion.identity);
Invoke("Spawn", Random.Range(minSpawnDelay, maxSpawnDelay));
}
}
解决方案
问题是因为您的 MeteorMover 中有以下内容:
public float speed = -2f;
因此,一旦创建了预制件,它的速度就会设置为 -2f。您需要在最后一颗流星被摧毁时获取它的速度,并将该速度用于您实例化的下一个流星。
推荐阅读
- dependency-injection - Redux-saga:无法访问 saga 中的类实例?
- ceph - centos7 上的 ceph-deploy install --release octopus 尝试加载不存在的 ceph-release-1-0 并且 ceph-deploy 不喜欢 centos8
- c# - 使用 EF Core 如何进行简单的连接以获得一个值
- loops - Lua 表 // 循环 // 哇插件 // 索引作为变量
- visual-studio - Visual Studio 2019 CS8032 无法从 C:\folder 创建 Microsoft.EntityFrameworkCore.InternalUsageDiagnosticAnalyzer 的实例
- python - 当我使用 PM2 时,我的 NodeJS 子进程没有运行我的 python 文件,但是当我在 nodemon 中执行它们时工作正常
- postgresql - 如果行的顺序在 posgres 中有聚集索引,是否按顺序锁定整个表?
- javascript - 如何在处理下一步之前等待 HTTP 请求在循环中获得响应
- android - Jetpack Compose + Firebase 崩溃 java.lang.ArrayIndexOutOfBoundsException 在 androidx.compose.runtime.SlotTableKt.groupSize(SlotTable.kt:2558)
- php - 如何在 voyager laravel 管理面板中添加批量操作