Search Unity

[Figured it Out] Pedulum like 2D controllable motion.

Discussion in 'Scripting' started by Venatal, May 2, 2016.

  1. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    I'm lost on how I should go about doing so.
    At the moment I'm using the Distance joint 2d along side my controller script
    Code (CSharp):
    1.  
    2. public float SwaySpeed = 5.0f;
    3.  
    4.     void Update()
    5.     {
    6.         if (Input.touchCount > 0)
    7.         {
    8.             Touch touch = Input.GetTouch(0);
    9.             if (touch.position.x < Screen.width / 2)
    10.             {
    11.                 transform.Translate(-Vector2.right * SwaySpeed * Time.smoothDeltaTime);
    12.             }
    13.             else if (touch.position.x > Screen.width / 2)
    14.             {
    15.                 transform.Translate(Vector2.right * SwaySpeed * Time.smoothDeltaTime);
    16.             }    
    17.         }
    18. }
    19.  
    which simply should move the player to the right when I press on the right side of the screen and visa versa however with the 2d physics enabled when momentum builds and the player is moving to the right for example I then cannot properly move to the left and so fourth.
    So How could I go about creating a script that when I press the right side of the screen the player travels in pendulum like motion till it reaches a max value i.e reaches screen boundary and then falls back to the lowest point when there is no touches registered and visa versa.
    P.S I plan not to use RigidBody2d.
    Thanks, if you need any additional information or want me to reword something let me know, thanks!
    Concept.png
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Pendulum motion over short distances is described reasonably well with a sine curve.