如何从不同的脚本更改此值?团结c#


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform : MonoBehaviour
{
public float speed = 10.0f;
private Rigidbody2D rb;

// Use this for initialization
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(-speed, 0);
}
// Update is called once per frame
void Update()
{
//not important
}
private void OnTriggerEnter2D(Collider2D other)
{
//not important
}
}

^ ^这是我的平台。cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformSpawner : MonoBehaviour
{
public GameObject Platform;
public GameObject Fireball;
public float respawnTime = 0.1f;
// Use this for initialization
void Start()
{
StartCoroutine(ObjectSpawning());
}
private void spawnPlatform()
{
//not important
}
private void spawnFireball()
{
//not important
}
IEnumerator ObjectSpawning()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnPlatform();
spawnFireball();
respawnTime *= 1.02f;
}
}
}

^ ^这是我的PlatformSpawner.cs

我是unity和c#的新手,如何改变速度Platform.csPlatformSpawner.cs? 我在网上查了一下,但我似乎找不到答案……所以我希望你们能帮助我!顺便说一句,我正试图逐渐增加速度值。提前谢谢

您是想仅为一个平台更改速度,还是为所有平台更改速度?

如果你想在所有平台上获得相同的速度,你应该将speed设置为静态。

public static float speed = 10.0f;

那么你可以像这样调节速度。

Platform.speed = 15.0f; //replace 15.0f with your desired speed.

如果你想改变特定平台的速度,从游戏对象中获取Platform组件,并修改速度。

// assuming "platform" is of type gameObject
platform.GetComponent<Platform>().speed = 15.0f; // replace 15.0f with your desired speed.

最新更新