I want to slow down all the RedBall and BlueBall by half in my game in 11 seconds.(when pros_control==ture) But for unknown reason, it only work in a redball in the game(I want all the balls can work).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tragger_red : MonoBehaviour
{
public GameObject MusicDraw;
public GameObject redpart;
public static float startForce = 3f;
private float distance;
Rigidbody2D rb;
GameObject[] backgroundObjects1;
GameObject[] backgroundObjects2;
private void Start()
{
backgroundObjects1 = GameObject.FindGameObjectsWithTag("BlueBall");
backgroundObjects2 = GameObject.FindGameObjectsWithTag("RedBall");
rb = GetComponent<Rigidbody2D>();
rb.AddForce(-(transform.up) * startForce, ForceMode2D.Impulse);
}
IEnumerator wait()
{
yield return new WaitForSeconds(11);
}
void Update()
{
foreach (GameObject f in backgroundObjects2)
{
Rigidbody2D rb = f.GetComponent<Rigidbody2D>();
if (f != null)
{
Vector3 fVelocity = rb.velocity;
if (pros_control.ice == true)
{
Debug.Log("fVelocity=" + fVelocity);
float f_vx = rb.velocity.x;
float f_vy = rb.velocity.y;
rb.velocity = new Vector2(f_vx * 0.5f, f_vy * 0.5f);
Debug.Log("f.GetComponent<Rigidbody2D>().velocity=" + rb.velocity);
StartCoroutine(wait());
pros_control.ice = false;
}
else
{
rb.velocity = fVelocity;
}
}
}
foreach (GameObject g in backgroundObjects1)
{
Rigidbody2D rb = g.GetComponent<Rigidbody2D>();
if (g != null)
{
Vector3 gVelocity = rb.velocity;
if (pros_control.ice == true)
{
Debug.Log("gVelocity=" + gVelocity);
float g_vx = rb.velocity.x;
float g_vy = rb.velocity.y;
rb.velocity = new Vector2(g_vx * 0.5f, g_vy * 0.5f);
Debug.Log("g.GetComponent<Rigidbody2D>().velocity=" + rb.velocity);
StartCoroutine(wait());
pros_control.ice = false;
}
else
{
rb.velocity = gVelocity;
}
}
}
}
}