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

Moving Game Objects

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

  1. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    I'm very new when it comes to unity and I need some help when it comes moving game objects. I want a game object to randomly spawn in a certain area and be able to move on its own in one direction. I have the "moving on its own" part figured out, but the game object will start out slow and increasingly get faster. I want the game object to stay at the same speed the entire time from the moment it is spawned. Any help or ideas on this? The code I used for the object to move is the following:

    rigidbody.AddForce(new Vector3(0, 50, 0) * Time.deltaTime);

    Please respond with anything that may be of use
     
  2. Buddy_Redmond

    Buddy_Redmond

    Joined:
    Aug 30, 2014
    Posts:
    23
    The problem is that adding a force will increase the velocity EVERY time the code runs. You can simply set the velocity in the start method:
    Code (CSharp):
    1. void Start() {
    2.     rigidbody.velocity = new Vector3(0f, 50f * Time.deltaTime, 0f);
    3. }
     
    MichaelHotte likes this.
  3. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    Thanks that worked!! Sorry i'm obviously new and i'm still learning!