unity 3d student

Beginner B08 – Basic Force Movement

How to move a Rigidbody object by applying a force.

Code Used

//Basic force to move a rigidbody object
  1. var power : float = 500.0;
  2.  
  3. function Start () {
  4.  rigidbody.AddForce(Vector3(0,0,power));
  5. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

30 Comments

    Hi! Great vid BTW! i really like it!

  • Very nice website! I like the way you say ‘force’

    • Thanks.. I think.. 🙂

    I edited the script so it would be easier to re-use it for another time

    //Basic force to move a rigidbody object in any direction

    var xForce : float = 0;
    var yForce : float = 0;
    var zForce : float = 0;

    function Start () {
    rigidbody.AddForce(Vector3(xForce,yForce,zForce));
    }

    thanks for the tutorial, it helped me a lot;)

    • I may well come back and borrow that from you at some point. Looks to be very handy to get used to that.

    • Cool idea Simen, I think you could probably just expose a single var as a Vector3 though, right? Then just apply that force.

    where is the level design? scripting is good, but i want to see terrain, trees, buildings, HDR sky, sun, or moonlight, shadows? where this tutorials?

    • Well you’ve missed the point of this approach a little – this is to promote original thinking in learning of game development, and picking up Unity to make original games without ideas being moulded by any particular format. I go into detail on terrain and level design in my book Unity Game Development Essentials – See here

    @script RequireComponent (Rigidbody)
    Should this line not have been added to the script to ensure a Rigidbody component is attached to the scripted object?

    • Technically yes, but as this is beginner stuff I hope that people will learn just as well from picking up the error that informs them that they need one if they forget – but you’re completely right that’s best practice.

    I would like to know why do we address rigidbody in lowercase and not rigidBody or RigidBody.

    what are the differences?

    Thanks

    • The reason its all lower case is so the coding script is easier to understand and so the compiler does not have to worry about one thing being capitilized and the rest lowercased. It is basic coding practice to have things lowercased most of the time.

    You commented in the video about applying the force to start when a button is pushed. However, to get the button to register requires:

    function Update(){

    Doesn’t it? Or am I missing something? I want to be able to push the ‘k’ button, and the force to be applied, but if I set it to Update my object goes whoooosh (as you’d expect).

    Can I nest functions within other functions or something to get around this?

    • Nevermind, I figured it out using:

      =======================================
      var power : float = 300.0;
      var lift : float = 300.0;
      var angle : float = 50.0;
      var useCount : int = 0;

      function Update () {
      if (Input.GetButtonUp(“Kick”))
      {
      if (useCount == 0)
      {
      rigidbody.AddForce(Vector3(angle, lift, -power));
      }
      useCount += 1;
      }
      }
      =======================================

      If you want to post up a mashed up comment to explain to others, please, feel free.

      • Cool yeah man people miss that using Up – the release of a button is crucial to an action occurring just once, well done!

    Hey will great video, will you be posting more? I love these easy to follow tutorials!

    • Yes! soon as I can, sorry for the delay guys.

    Hello I was wondering how I can define “Enter key”. Here is what I did:

    __________________________________________________________
    function Update () {

    if(Input.GetButtonUp(Enter)){
    Debug.Log(“User have pressed the Enter”);
    }
    }
    __________________________________________________________

    Then I want to do this, if possible:

    __________________________________________________________
    var power : float = 500.0;

    function Update () {
    if(Input.GetButtonUp(Enter)){
    rigidbody.AddForce(Vector3(0,0,power));
    }
    }
    __________________________________________________________

    My thx in advance.

    • Return and Enter are both valid terms, and differing keys – plus you need to write them in lowercase inside inverted commas, like this – “enter” – and use GetKey instead, see here –

      Input Manual

    Thx for the videos! But i’m curious in one point. In the video you say “it is applied only once”. but force is not that kind of thing, force applies for a length of time.

    I thought maybe it is impulse (push) what we are talking about here. If we make the mass bigger, the velocity reduces right? so it must be impulse, not force i believe. Or am i wrong ? does this Force Vector applies for a length of time ?

    • No you’re just getting bogged down by terminology from other places – in this context, the Force is applied once. However the ‘forces’ of the physics engine in general are always acting on the object, naturally, so you could argue its a bad thing to call it, but that’s just what the API calls this action, so long as you consider a force (singular) to be an impulse then yes, its once. You can see in the menu there is a constant force component which adds this, you also have force modes (see script ref) that you can add to this command’s arguments, and naturally you can also adjust the velocity (speed and direction) of a rigidbody at any time.

    • I had exactly the same question. From the script reference, I can see that you can apply a parameter at the end that lets you choose what kind of Force you want, and Impulse is one of those options. ForceMode.Force does indeed define force as mass*distance/time^2, so there is indeed a time component. I can only GUESS that the time mentioned is the delta time for a single frame. The script reference recommends that if you want to apply a Force over several frames to use the FixedUpdate callback rather than the Update callback, presumably because FixedUpdate gets called with a constant delta time for each frame.

    trully thx !
    i’ve been looking for this in forums, videos and tutotials for 2 days now !
    really thx 🙂

  • How do I add a force to a game objects transform. What I am trying to do is spawn a game object and send it launching forward but when I spawn the object it dose not go forward. How would I do this

    • that’s what this teaches.. you may be getting the direction wrong I guess? but either way you need a rigidbody component applied in order to use forces, and then you can simply use the forward direction of the object you’re launching from – presumably an empty one. Then in update say something like –

      if(Input.GetButtonDown(“Fire1”)){
      var shot : Rigidbody = Instantiate(rbPrefab, transform.position, transform.rotation);
      shot.AddForce(transform.forward*50);
      }

      where rbPrefab is your rigidbody prefab you’ve made and created a public variable for at the top of your script so it can be assigned in the inspector.

    C# code:

    using UnityEngine;
    using System.Collections;

    public class Force:MonoBehaviour
    {
    public float power = 500.0f;

    void Start()
    {
    rigidbody.AddForce(new Vector3(0,0,power));
    }// End start method
    }// End Force class

  • How do I do this with C#?

  • My relatives all the time say that I am wasting my time here at net, but I know I am getting familiarity daily by
    reading thes pleasant posts.

  • Hello from Russia!
    First of, thanks you for your big work! (and sorry for my english)
    I just want to say that your script doesn’t work in Unity 5!
    It’s two ways to solve this problem:
    1) Unity automatically replace one line like this:
    GetComponent.().AddForce(Vector3(0,0,power));
    2) To read manual and to write like this:
    //Basic force to move a rigidbody object
    var power : float = 500.0;
    var rb : Rigidbody;

    function Start () {

    rb = GetComponent.();
    rb.AddForce(Vector3(0,0,power));

    }

  • var speed : float = 5.0;

    function Update () {
    transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
    }

Leave a Comment