Hello!
I am pretty new to Unity and just testing it out.
I made a game where obstacles are falling from the sky and you have to dodge. The obstacles are spawning trough a prefab and having mass and gravityscale to increase their speed over time. Now i want to stop them getting faster from a point X. How do I do this? If the speed would be increased through a script this would be pretty simple for me, but i do not know how to access the speed without having a script so far
How is it that they get a higher gravity scale without a script? Different prefabs with different values or something else?
If having a script would be pretty simple for you, then just add a script - that seems the most obvious to me…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeSlow : MonoBehaviour {
Rigidbody2D rb;
//here the max speed, use negative
public float speed;
void Start () {
//link to rigidbody2D component
rb = GetComponent<Rigidbody2D> ();
}
void FixedUpdate () {
//check if moving vector on y-axis is lower (because of falling) as speed
if (rb.velocity.y < speed) {
//don't change x-axis, replace y-axis with needed speed
rb.velocity = new Vector2 (rb.velocity.x, speed);
}
}
}
1 Like
Thats perfect, ty!