Search Unity

Moving a platform from one point to another indefinitely HELP please

Discussion in '2D' started by videoanime, Oct 31, 2014.

  1. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Hi, can anybody help me? I've searched in 2D, in Scripting and even in the Answers and I don't figure out how to move an object up and down indefinitely.

    I have this code

    float dist = (Time.time)*speed;
    float fraccion = dist / length;
    transform.position = Vector3.Lerp (inicio, fin, fraccion);


    The movement goes smoothly from the upper point(inicio) to the lower one(fin), but I don't know how to invert it.

    I understood that "fraccion" must be a value between 0 and 1, 0 is for "inicio" and 1 is for "fin", the problem here is that "dist" is obtained multiplying by time and the time goes ahead, after it reaches 1 it keeps on. Here I need to alternate "fraccion" between 0 and 1 indefinitely.

    I tried with "for" but the problem with the instruccion "for" is that it executes completely in every frame and the movement is not at all smooth.
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    I made this script a while ago to have some object floating in a 2D game. It might suit your need.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class FloatingScript : MonoBehaviour
    6. {
    7.     public Vector2 randomSpeedRange;
    8.     public float speed;
    9.  
    10.     public Vector2 randomAmplitudeRange;
    11.     public float amplitude;
    12.  
    13.     Vector3 startPos;
    14.     bool isFloating = false;
    15.  
    16.     void Awake()
    17.     {
    18.         if(speed == 0)
    19.             speed = Random.Range(randomSpeedRange.x, randomSpeedRange.y);
    20.  
    21.         if(amplitude == 0)
    22.             amplitude = Random.Range(randomAmplitudeRange.x, randomAmplitudeRange.y);
    23.     }
    24.  
    25.     public void Reset()
    26.     {
    27.         transform.position = startPos;
    28.     }
    29.  
    30.     void Start()
    31.     {
    32.         startPos = transform.position;
    33.         StartFloat();
    34.     }
    35.  
    36.     void StartFloat()
    37.     {
    38.         isFloating = true;
    39.     }
    40.  
    41.     void Update()
    42.     {
    43.         if(isFloating)
    44.             transform.position = new Vector3(transform.position.x, (startPos.y + amplitude * Mathf.Sin(speed * Time.time)), transform.position.z);
    45.             // Since Sin ranges from -1 to +1, you'll have the object floating from -amplitude to +amplitude around the y0 coordinate,
    46.             // and the time to complete one cycle is 2 * PI * speed - about 6.3 seconds if speed is 1.
    47.     }
    48. }
     
  3. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Ahhhh ok, I undestand, "Sin" helps you to alternate, to go forwards and towards and "speed" as its name says indicates how fast is the movement, ok, thank you, it helps me a lot. As I see, "for" is useless in "update" and "fixedupdate".