Question by
turnjinxsay · Jun 22, 2017 at 09:27 PM ·
c#platformplatforms
my platform goes up but doesnt come back down?
okay so, i'm following a video because i'm doing the same project for my exam next week, and i can't seem to make the platform move as it should. here's what i've got:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class plataformaascendente : MonoBehaviour
{
float velocidadPlataforma = 2;
bool puntoLLegada;
float puntoPartida;
float puntoLLegadaY;
public int unidadesaMover;
// Use this for initialization
void Start()
{
puntoPartida = transform.position.y ;
puntoLLegadaY = puntoPartida + unidadesaMover;
}
// Update is called once per frame
void Update()
{
if (puntoLLegada)
{
transform.position += Vector3.up * velocidadPlataforma * Time.deltaTime;
}
else
{
transform.position -= Vector3.up * velocidadPlataforma * Time.deltaTime;
}
if (transform.position.y >= puntoLLegadaY)
{
puntoLLegada = false;
}
if (transform.position.y <= puntoLLegadaY)
{
puntoLLegada = true;
}
}
}
again, i'm sorry about the spanish, and thanks for taking the time to read through this!
Comment
Best Answer
Answer by turnjinxsay · Jun 22, 2017 at 09:54 PM
okay, someone already helped me, this is what worked:
public float speed;
public float distance;
public float downLimit;
public float upLimit;
public int dir = 1;
public float PositionActual;
public float PosicionStart;
void Start()
{
PosicionStart = transform.position.y;
downLimit = PosicionStart - distance;
upLimit = PosicionStart + distance;
speed = 5;
}
void Update()
{
PositionActual = transform.position.y;
transform.position += Vector3.up * speed * Time.deltaTime * dir;
if (PositionActual > upLimit)
{
dir = -1;
GetComponent<SpriteRenderer>().flipX = false;
}
else if (PositionActual < downLimit)
{
dir = 1;
GetComponent<SpriteRenderer>().flipX = true;
}
}
Your answer
Follow this Question
Related Questions
#if UNITY_ANDROID doesn't exists in unity 5 3 Answers
Doodle Jump Game Platform Spawn Issue Unity2D 0 Answers
Physics2D.OverlapCircle doesnt work 1 Answer
Help me with gui please (C#) 2 Answers