I wanted to share my solution for having multiple surface effectors in the scene

I saw someone else had the same problem so I wanted to share the solution

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

public class Player : MonoBehaviour
{

    Rigidbody2D rb2d;
    [SerializeField] float torqueForce = 1;
    [SerializeField] float boostSpeed = 30f;
    [SerializeField] float baseSpeed = 20f;
    SurfaceEffector2D[] effectors = new SurfaceEffector2D[3];

    // Start is called before the first frame update
    void Start()
    {
        
        rb2d = GetComponent<Rigidbody2D>();
        effectors = FindObjectsOfType<SurfaceEffector2D>();
        
    }

    // Update is called once per frame
    void Update()
    {
        RotatePlayer();
        RespondToBoost();
    }

    void RespondToBoost()
    {
       
        if (Input.GetKey(KeyCode.UpArrow))
        {
            foreach(SurfaceEffector2D effect in effectors)
            {
                effect.speed = boostSpeed;
            }
        }
        else
        {
            foreach(SurfaceEffector2D effect in effectors)
            {
                effect.speed = baseSpeed;
            }
        }
    }

    void RotatePlayer()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            rb2d.AddTorque(torqueForce);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            rb2d.AddTorque(-torqueForce);
        }
    }
}

5 Likes

Thank you for sharing the solution

This fixed my problem perfectly! Thanks.

Would you mind explaining what the first and second set of brackets do in the line
SurfaceEffector2D[] effectors = new SurfaceEffector2D[3];
Thank you again.

Privacy & Terms