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

Movements 2D not on input

Discussion in 'Scripting' started by Democrito94, May 6, 2014.

  1. Democrito94

    Democrito94

    Joined:
    Apr 8, 2014
    Posts:
    2
    Hey guys, I'm new on this forum and on Unity also.
    I've seen few topics dealing with these information but I have a problem that could not be considered part of coding.

    I'm studying C at school, so C# is still new to me.

    I know that movement is just an illusion made by a continuous replacement of the image in the next or previous space.
    What I'm asking for is how can I translate this to code?
    Mmmh. I take the initial position of my Game Object, and multiply it for a variable of my choice...all this in the "Update" function, right?
    And also multiply them for Time.deltaTime?

    Example:

    Code (csharp):
    1. void function Update
    2. {
    3. transform.position = transform.position * speed /*my variable initialised before*\ * Time.deltaTime /* To have it moving every second and not every frame*\;
    4. }
    Is it all?

    I'm a newbie.. sorry if I sound stupid or don't know what..

    Thanks in advance,
    Bye
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Code (csharp):
    1.  
    2. float speed;
    3. Vector3 direction;
    4.  
    5. void Start() {
    6.   // Picking random values.
    7.   speed = 10;
    8.   direction = Vector3.right;
    9. }
    10.  
    11. void Update() {
    12.   // Take current position, and add another Vector3 to it.
    13.   // The value we add is how far the object moves in a frame.
    14.   transform.position += speed * direction * Time.deltaTime;
    15. }
    16.  
    EDIT: For 2D, you can use Vector2 instead of Vector3.
     
  3. Democrito94

    Democrito94

    Joined:
    Apr 8, 2014
    Posts:
    2
    Thank you so much! So I have to add to transform.position not to multiply it. Thank you again :)
    Sorry for your waste of time..
    Bye :)