Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to change Velocity2D as time goes on

Discussion in 'Scripting' started by lionelscotty, Feb 23, 2015.

  1. lionelscotty

    lionelscotty

    Joined:
    Aug 5, 2014
    Posts:
    6
    I want the the x component of the velocity to increase every 5 seconds, but the velocity doesn't stay at the increased value it always reverts to the original -8 .

    Can someone please please please help me with this problem? The help is greatly greatly appreciated.

    public class Obstacle : MonoBehaviour {

    public Vector2 velocity = new Vector2(-8,0);


    public float range = 4;

    // Use this for initialization
    void Start () {
    rigidbody2D.velocity = velocity;

    transform.position = new Vector3 (transform.position.x, transform.position.y - range*Random.value ,
    transform.position.z);

    transform.localScale = new Vector3 (Random.Range (5, 10), Random.Range (1, 5), transform.localScale.z); //chage scale but not working.
    }

    // Update is called once per frame
    void Update () {


    if(Time.time%5==0){
    velocity = velocity*1.5f;
    rigidbody2D.velocity = velocity;
    }

    Vector2 obstaclePosition = Camera.main.WorldToScreenPoint (transform.position);

    //Destroys dirt when off screen.
    if (obstaclePosition.x < Screen.width && obstaclePosition.x < -5) {
    Destroy(this.gameObject);
    }



    }




    }
     
  2. LividPixel

    LividPixel

    Joined:
    Feb 21, 2015
    Posts:
    11
    I'm not too sure if this is the exact cause of your problem, but I don't believe that your 'if' statement will work as you intend.

    Code (CSharp):
    1. if(Time.time%5==0)
    This will only be true when Time.time is a perfect multiple of 5, which is generally unlikely, I believe. You'll probably want a different approach.

    You could have a float for '_timeOfLastVelocityChange' or something similar, and have something like so:

    Code (CSharp):
    1. if (Time.time + _timeOfLastVelocityChange >= 5)
    2. {
    3.     velocity *= 1.5f
    4.     rigidbody2D.velocity = velocity;
    5.     _timeOfLastVelocityChange = Time.time;
    6. }
    This will ensure that your code is run once every five seconds.
     
  3. lionelscotty

    lionelscotty

    Joined:
    Aug 5, 2014
    Posts:
    6
    Thank you for your response!!
    It works every 5 seconds but the objects speed up too quickly, what should I do?
     
  4. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    LividPixel's idea is in the right direction but Time.time + _timeOfLastVelocityChange will
    just become too high too soon. The usual way is something like this :


    Code (CSharp):
    1. if (_timeOfLastVelocityChange - Time.time >= 5)
    2. {
    3.     velocity *= 1.5f
    4.     rigidbody2D.velocity = velocity;
    5.     _timeOfLastVelocityChange = Time.time;
    6. }
    and add this line in Start() or Awake()

    Code (CSharp):
    1. _timeOfLastVelocityChange = Time.time;
     
  5. LividPixel

    LividPixel

    Joined:
    Feb 21, 2015
    Posts:
    11
    This code will never fire, because Time.time will always be greater than _timeOfLastVelocityChange, as _timeOfLastVelocityChange is only set to Time.time, and Time.time is always increasing, but _timeOfLastVelocityChange is not. Because of this, your if statement will never be true.
     
  6. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    oh yeah my bad, I typed that in a hurry. Just need to reverse the subtraction :

    Code (CSharp):
    1. if (Time.time - _timeOfLastVelocityChange >= 5)
     
  7. lionelscotty

    lionelscotty

    Joined:
    Aug 5, 2014
    Posts:
    6
    Umm thank you for the replies but now it's back to original state it's not speeding up at all now. This is what I have

    public Vector2 velocity = new Vector2(-8,0);
    float _timeOfLastVelocityChange;

    public float range = 4;

    // Use this for initialization
    void Start () {
    rigidbody2D.velocity = velocity;
    _timeOfLastVelocityChange = Time.time;
    transform.position = new Vector3 (transform.position.x, transform.position.y - range*Random.value ,
    transform.position.z);

    transform.localScale = new Vector3 (Random.Range (5, 10), Random.Range (1, 5), transform.localScale.z); //chage scale but not working.
    }

    // Update is called once per frame
    void Update () {
    SpeedUp ();
    Vector2 obstaclePosition = Camera.main.WorldToScreenPoint (transform.position);

    //Destroys dirt when off screen.
    if (obstaclePosition.x < Screen.width && obstaclePosition.x < -5) {
    Destroy(this.gameObject);
    }



    }


    void SpeedUp(){
    if( Time.time-_timeOfLastVelocityChange >=5 ){
    velocity *=100f;
    rigidbody2D.velocity = velocity;

    }
    }
     
  8. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Yeah you need to store the timestamp every time the if clause executes otherwise
    after 5 seconds it will run on each Update() and also try a Debug.Log of the velocity.
    like in the code below. If velocity is increasing, then there could be some issue
    with the Physics stuff :

    Code (CSharp):
    1. void SpeedUp(){
    2.   if( Time.time-_timeOfLastVelocityChange >=5 ){
    3.     velocity *=100f;
    4.     rigidbody2D.velocity = velocity;
    5.     _timeOfLastVelocityChange = Time.time; // Timestemp !
    6.     Debug.Log("New Velocity : " + velocity); // Check this log
    7.   }
    8. }
     
    lionelscotty likes this.
  9. lionelscotty

    lionelscotty

    Joined:
    Aug 5, 2014
    Posts:
    6
    Okay the Debug.log says the velocity becomes -800 in the Console, but I realize the GameObjects don't speed up. They are still moving at -8, and also the -800 doesn't increase , I thought it would increment? I read in a book a good way to increase difficulty is like to store the speed values or w/e in an array but I don't know how to go about that. I don't really know C# well enough yet.
     
    Last edited: Feb 24, 2015
  10. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Ok, I don't have much experience with Unty physics, but I think instead of setting
    velocity directly, you can set the force instead. Like this is what the manual
    says :

     
  11. lionelscotty

    lionelscotty

    Joined:
    Aug 5, 2014
    Posts:
    6
    Well the things velocities that I want to have changed are not gained by having forces applied. Maybe I can just make the objects translate by not using the physics engine and just make it move by changing the x positions but can i do that without using the physics engine?
     
  12. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Yes, you can.
    If you don't want to use collisions, then you can just translate..in which
    case you can remove the rigidbody and collider as well.
     
  13. lionelscotty

    lionelscotty

    Joined:
    Aug 5, 2014
    Posts:
    6