Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Problem with basics, Vector3, AddForce

Discussion in 'Getting Started' started by malenacorl, Mar 30, 2015.

  1. malenacorl

    malenacorl

    Joined:
    Mar 30, 2015
    Posts:
    3
    Hello, I'm trying to understand following lines:

    float moveH += 10.0f * Random.value - 5.0f;
    moveV += 10.0f * Random.value - 5.0f;
    Vector3 movement = new Vector3 (moveH,0, moveV);
    rb.AddForce (movement);

    and I don't really get it: what is the value 5.0f? Is something about accelaration? I thought "10.0f" is speed, but increasing the value doesn't make sphere move faster (in fact it sometimes moves faster with lower value than 10.0f)
    And one more thing:
    rb.AddForce(new Vector3(0,0,0));
    what this line exactly does? Doesn't Vector3(0, 0, 0) disable move in any of 3 dimensions?
     
  2. skeleton-king

    skeleton-king

    Joined:
    Nov 10, 2014
    Posts:
    63
    5.0f is just a constant speed that u substract to the product of 10 and random range.
    whether the sphere moves slower or faster depends upon the random.value generated. So if ur random.value is higher it would proabably move faster
    rb.AddForce(new Vector3(0,0,0)); doesnt add any force to your sphere.
     
  3. malenacorl

    malenacorl

    Joined:
    Mar 30, 2015
    Posts:
    3
    Thank you for your answer, so it means person who wrote this code was just trying to achieve certain speed by substracting and multiplying by those values? And it isn't any deeper sense in this? :)
     
  4. Effervescent

    Effervescent

    Joined:
    Mar 7, 2015
    Posts:
    31
    I suppose there is a little bit more to it because one may ask why not just do:

    Code (CSharp):
    1. public float speedH;
    2. private float moveH;
    3.  
    4. // Things in between
    5.  
    6. moveH = speedH * Random.value;

    In the example above moveH is either 0 or, else, it always retains the sign from speedH because Random.value is either 0 or positive (up to and including 1). On the other hand, if you factorise the line for moveH from your code, it becomes:

    Code (CSharp):
    1. moveH += 5.0f * (2.0f * Random.value - 1.0f);

    What the bit in parentheses is trying to achieve is essentially to generate a number randombly between -1 to +1. It's worth noting that the same can be achieved by Random.Range and, with that, the code would simply become:

    Code (CSharp):
    1. moveH += Random.Range(-5.0f, 5.0f);
     
    Last edited: Mar 30, 2015
  5. malenacorl

    malenacorl

    Joined:
    Mar 30, 2015
    Posts:
    3
    Thank you for your answer. As a total beginner, can I ask why one wants to achieve a number between -1 to +1? What if Vector3 had numbers below or over -1 and +1 as its x and z?
     
  6. Effervescent

    Effervescent

    Joined:
    Mar 7, 2015
    Posts:
    31
    Hmmm... since I have no idea what the original code is meant for I'll use the following example: let's suppose you have a game in which the character controlled by the player has to shoot a bunch of randomly-generated bubbles (and probably not get touched!); if the bubbles are always moving towards the positive direction of the x- and z-axes, its would look kind of silly (or at least at the beginning if you have some sort of boundaries).

    To make it more unpredictable (and to make it look less silly!) you would want the bubbles to be moving at different velocities, in other words, different speeds and directions, in both the positive and negative directions and you would *maybe* use something like that. I said maybe because, in that case, I personally would used Rigidbody.velocity/Rigidbody2D.velocity; the bubble scenario I've chosen clearly isn't the best but you get the idea! xD

    As for whether it's below or above -1 and +1, respectively, you need to first understand the concept of vectors (it's not difficult!). I'll just leave you a picture because it'd take a while to explain properly and there are better resources for learning vectors properly (you could try the linear algebra tutorial @ninja_pat posted a couple of weeks ago).

    Note that I've done it only for the x-z plane to simplify the illustration but the idea is the same even if the y component is non-zero; also, the numbers in circles are called the magnitudes of the vectors - in the case of Rigidbody.velocity that corresponds to the speed of the object moving towards the indicated direction.