Search code examples
c#unity-game-engine

Trying to Increase move speed of objects in unity with C#


trying to increase movement speed of objects that spawn, I can only get the very first object to increase in speed at the time the function runs and all the other objects remain the same speed

this is the script to spawn the objects. It is set on its own game object. I am new to C#

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

public class PipeSpawnerScript : MonoBehaviour
{
    public GameObject pipe;
    public float spawnRate = 2;
    public float timer = 0;
    public float hightOffset = 10;
    public Birdscript Bird;
   

    // Start is called before the first frame update
    void Start()
    {
        spawnPipe();
        Bird = GameObject.FindGameObjectWithTag("Bird").GetComponent<Birdscript>();
        
    }

    // Update is called once per frame
    void Update()
    {
        if (timer < spawnRate)
        {
            timer = timer + Time.deltaTime;
        }
        else
        {
            if (Bird.birdIsAlive)
            {
               
                spawnPipe();
                timer = 0;

            }
            
        }
    }
    void spawnPipe()
    {
        float lowestPoint = transform.position.y - hightOffset;
        float highestPoint = transform.position.y + hightOffset;

        Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
    }
}

This is the script on the actual pipe object itself. I am trying to change the variable "move speed"

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

public class pipeMoveScript : MonoBehaviour
{
    public float moveSpeed = 5;
    public float deadZone = -40;
    public Birdscript Bird;
    public float speedtime = 10;
    private float timer = 0;

 
   
    // Start is called before the first frame update
    void Start()
    {
        Bird = GameObject.FindGameObjectWithTag("Bird").GetComponent<Birdscript>();
    }
    public void Speedup(int speedToadd)
    {
        moveSpeed += speedToadd;
    }
    // Update is called once per frame
    void Update()
    {
        if (timer < speedtime)
        {
            timer += Time.deltaTime;
        }
        else
        {
            Speedup(5);
            timer = 0;
        }

        if (Bird.birdIsAlive)
        {
            transform.position = transform.position + (Vector3.left * moveSpeed) * Time.deltaTime;
        }

        if (transform.position.x < deadZone)
        {
            Debug.Log("pipe Deleted");
            Destroy(gameObject);
        }
       
    }

}

what I have added to increase the movement speed is this. as well as the function "speedup"

 if (timer < speedtime)
        {
            timer += Time.deltaTime;
        }
        else
        {
            Speedup(5);
            timer = 0;
        }

Solution

  • if i understood correctly, you want to speed up all of objects, including those who are already spawned and those who will be spawned. take this approach. 1- make a gameobject with a script on it that has a variable "currentGameSpeed" 2- then in update function of your pipe find this script (find object of type) and read the current speed from that.

    ps: if you are concerned about performance,then just read the speed in start function and then when you want to speed up, find all of pipes in the scene using find objectS of type and change their speed. but your game is pretty simple and you wont have any problem with first approach.