Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to Move An Object Along Y-Axis from Two points and Loop It?!

Discussion in '2D' started by anuragnaidu204, Feb 16, 2020.

  1. anuragnaidu204

    anuragnaidu204

    Joined:
    Feb 9, 2020
    Posts:
    1
    How to Move An Object Along Y-Axis from Two points and Loop It?!
    I'm New to UNITY 2D AND I'm trying to figure out how to move and object from point A to point B along Y axis (top to bottom)
    i want to move the block up and down
    (just like in the mario 2d games where the block goes up and down )
    I hope someone can script this down for me thank you!:)
     

    Attached Files:

  2. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    For simple movement use setup on image below and code for PingPongMove script.

    SimpleMovement.jpg

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PingPongMove : MonoBehaviour {
    4.  
    5.     [SerializeField] private Vector2 _start = Vector2.zero;
    6.     [SerializeField] private Vector2 _end   = Vector2.zero;
    7.     [SerializeField] private float   _speed = 1f;
    8.  
    9.     // ------------------------------------------------------
    10.     void Update() {
    11.  
    12.         float t = Mathf.PingPong(Time.time, _speed) / _speed;
    13.  
    14.         transform.position = Vector2.Lerp(_start, _end, t);
    15.     }
    16. }
    You can easily use Unity's Mathf.PingPong method that converts time from game start to value between 0-x forever (for x we use _speed). We are dividing result with _speed to get it into 0-1 range, which we need for Lerp method.

    But, big warning! From your picture it looks like you need this for moving platform, which should be physics object... If so, then you shloud add RigidBody2D and Collider components and move it not in Update(), but in FixedUpdate(). And you should not move it by changing Transform, but setting new position of RigidBody2D. Setting transform for RigidBody2D is like teleporting it.

    Here is setup for such object and below is code (notice - Update() changed to FixedUpdate()):

    WithPhysics.jpg

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PingPongMove : MonoBehaviour {
    4.  
    5.     [SerializeField] private Vector2 _start = Vector2.zero;
    6.     [SerializeField] private Vector2 _end   = Vector2.zero;
    7.     [SerializeField] private float   _speed = 1f;
    8.  
    9.     private Rigidbody2D _rigidBody = null;
    10.  
    11.     // ------------------------------------------------------
    12.     private void Awake() {
    13.  
    14.         _rigidBody = GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     // ------------------------------------------------------
    18.     void FixedUpdate() {
    19.  
    20.         float t = Mathf.PingPong(Time.time, _speed) / _speed;
    21.  
    22.         _rigidBody.position = Vector2.Lerp(_start, _end, t);
    23.     }
    24. }
    25.  
    Just for fun: if you want nice movement (object slows down and speeds up), you can add this line into FixedUpdate() to alter t before it is used in Lerp:

    Code (CSharp):
    1.         float t = Mathf.PingPong(Time.time, _speed) / _speed;
    2.         t = (-Mathf.Cos(Mathf.PI * t) + 1) / 2;
    3.  
    What it does is, it converts linear value between 0-1 to cosine value for PI period. But cosine for PI is in range 1 to -1, so we multiply it by -1 to get -1 to 1, Then we add 1 to change it into 0 to 2. And in the end we divide it by 2 to get 0-1 again.
     
    anycolourulike likes this.