0
\$\begingroup\$

I'm new to Unity, and am making a game where an object goes up, falls down and is destroyed. This object has been instantiated. I want to increase the object movement speed by 1 after 10 seconds, but not by more than 12, because the object moves off screen.

How do I do that? This is what I have, so far:

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

public class DestroyScript : MonoBehaviour
{
    public Rigidbody2D rb;
    private float destroyTime = 2.0f;
    private int speed = 8;  

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        Destroy(gameObject, destroyTime);             
    }  

    void FixedUpdate()
    {
        transform.Translate(Vector2.up * speed  * Time.deltaTime, Space.World);
        transform.Rotate(0, 0, 1);                           
    }   
}

Thank you for your answer and sorry for my incomplete question actually object are spawned. here is the spawned script

public class SpawnItems : MonoBehaviour {

    public Transform[] SpawnPoints;     

    public GameObject[] Coins;

    float createRate = 1.5f, createRateTimer;
    float rateIncrease = 0.1f, intialCreateDelay = 1.0f;
    int callCounter = 0, callBeforeRateIncrease = 15;    

    // Use this for initialization
    void Start () {
        createRateTimer = createRate + intialCreateDelay; 
    }

    private void Update()
    {
        createRateTimer -= Time.deltaTime;
        if(createRateTimer <= 0)
        {
            CustomInvoke();
        }
    }

    void CustomInvoke()
    {
        int spawnIndex = Random.Range(0, SpawnPoints.Length);
        int objectIndex = Random.Range(0, Coins.Length);
        Instantiate(Coins[objectIndex], SpawnPoints[spawnIndex].position, SpawnPoints[spawnIndex].rotation);
        callCounter++;
        if(callCounter >= callBeforeRateIncrease)
        {
            createRate -= rateIncrease;
            callCounter = 0;
        }
        createRateTimer = createRate;
    }      
}
\$\endgroup\$
  • \$\begingroup\$ Please consider visiting this page to have your accounts merged. This will allow you to edit your own posts, comments on posts on your questions and accept the answer that helped you the most. \$\endgroup\$ – Vaillancourt Sep 4 '17 at 11:33
2
\$\begingroup\$

Just a little tip: have you tried using 'Rigidbody.AddForce()' ? all you have to do is use a 'Vector3()' and specify the axis and the force will be applied to the object. like this:

public float force = 10f;
public float multiplier = 1000f;

void FixedUpdate()
{
    //during the update method move the object on the Y axis with a force "force"
    Rigidbody rb;
    rb.AddForce(new Vector3(0,force * Time.fixedDeltaTime * multiplier,0)); 
}

this is just a tip, because this is driven by physics instead of translating the object, which is not as smooth as actually moving the rigidbody! :) this is not really the answer but hope it helps better your game!

\$\endgroup\$
  • 1
    \$\begingroup\$ When you're adding a force, you don't need to multiply it by deltaTime. The AddForce method integrates it over the time step already. If you double-dip, then you can get inconsistent results if you later change your fixed time step. \$\endgroup\$ – DMGregory Nov 8 '17 at 2:43
  • \$\begingroup\$ Okay got it! I didn't know that integrated it already \$\endgroup\$ – user100681 Nov 8 '17 at 6:05
1
\$\begingroup\$

Increase the object movent speed by 1 after 10 seconds is equal to increase speed by 0.1 after 1 second. You can also call speed increase "acceleration"

so add acceleration declaration :

   private float acc = 0.1; 

make speed a float

   private float speed = 8;  

increase speed in FixedUpdate

void FixedUpdate()
{
    speed  = speed  + acc * Time.deltaTime;
    transform.Translate(Vector2.up * speed  * Time.deltaTime, Space.World);
    transform.Rotate(0, 0, 1);                           
}

finaly for the

but not by more than 12, because the object moves off screen.

i think you can set destroyTime to 12.0f instead of 2.0f;

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.