Search Unity

transform.position Mathf.Sin

Discussion in 'Scripting' started by filecity, Oct 13, 2009.

  1. filecity

    filecity

    Joined:
    Apr 21, 2009
    Posts:
    117
    jhasenau likes this.
  2. joseprando

    joseprando

    Joined:
    Sep 2, 2009
    Posts:
    3
    Hi

    There's a script in the 3D Platform Tutorial to handle this, search for the LaserTrap in the document.


    See ya
     
  3. filecity

    filecity

    Joined:
    Apr 21, 2009
    Posts:
    117
    Thanks. I'm getting the object to move up and down now. But, how do I make it move in x-axis. I can't get it move in Time.deltaTime.

    Code (csharp):
    1.  
    2. var height = 3.2;
    3. var speed = 2.0;
    4. var timingOffset = 0.0;
    5. var count=0;
    6.  
    7. function Update()
    8. {
    9. count++;
    10.     var offset = Mathf.Sin(Time.time * speed + timingOffset) * height / 2;
    11.     transform.position = Vector3(count, offset, 0);
    12.  
    13. }
    14.  
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The code you are using moves the object in the X axis by one unit each frame. To make it move at one unit per second, use something like this:-
    Code (csharp):
    1.  
    2.    ...
    3. var count=0.0;
    4.  
    5. function Update()
    6. {
    7.    count += Time.deltaTime;
    8.     var offset = Mathf.Sin(Time.time * speed + timingOffset) * height / 2;
    9.     transform.position = Vector3(count, offset, 0);
    10.  
    11. }
    12.  
     
    Ploopploop and jhasenau like this.
  5. filecity

    filecity

    Joined:
    Apr 21, 2009
    Posts:
    117
    wow thanks. That's what I'm looking for.
     
  6. screenname_taken

    screenname_taken

    Joined:
    Apr 8, 2013
    Posts:
    663
    Aaaand some years after, it's also what i was looking for.
     
  7. elopez7

    elopez7

    Joined:
    Mar 8, 2014
    Posts:
    19
    And even from farther ahead in the future, this is what I am looking for.
     
  8. jhasenau

    jhasenau

    Joined:
    Aug 22, 2017
    Posts:
    1
    Every two years someone comes across this post and is helped. Thank you for the great question, and the great answer!
     
  9. NyleLevi

    NyleLevi

    Joined:
    Mar 12, 2018
    Posts:
    1
    I think this has just solved my problem too! Thanks!
     
  10. Ploopploop

    Ploopploop

    Joined:
    Oct 15, 2017
    Posts:
    4
    A bit over 9 years later it also solved my issue, cheers!
     
  11. tom_painter

    tom_painter

    Joined:
    Nov 8, 2018
    Posts:
    13
    woooo I time travelled to 2019 and got helped by this. thanks