- Home /
Question by
Borr1310 · Oct 21, 2016 at 01:44 PM ·
timevelocityspeedinvokerepeating
Increase speed every 5 seconds
Why would this not work?
public static float speed = -5f;
public Vector2 obstacleVelocity = new Vector2(0.0f, speed);
public Rigidbody2D rb;
// Use this for initialization
void Start()
{
InvokeRepeating("IncreaseSpeed", 3, 1);
rb.velocity = obstacleVelocity;
transform.position = new Vector3(Random.Range(transform.position.x - 2.2f, 2.2f), transform.position.y + 2, transform.position.z);
}
void Update()
{
Destroy(gameObject, 5);
}
void IncreaseSpeed()
{
speed = speed - 5f;
}
I know there is a number of ways to do this, but what I tried to do is assign the Y value to -5, and then icrease Y (speed) every 3 seconds for -0.3f. It doesn´t seem to work, the objects are not getting any faster.
Thanks!
Comment
Best Answer
Answer by hexagonius · Oct 21, 2016 at 04:50 PM
you need to assign the newly calculated velocity to the rigidbody in IncreaseSpeed(). Vector2 is a value type, not a reference type, which means, the value is copied to velocity not referenced and can therefore not be "updated".
Your answer
Follow this Question
Related Questions
Gravitational pull without losing speed 1 Answer
increasing speed and time of spawn game object 0 Answers
Make an object move forward on its own 2 Answers