Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

[Aporte] PlatformMove

Iniciado por GSG3D, Abril 11, 2016, 08:55:52 AM

Tema anterior - Siguiente tema
Abril 11, 2016, 08:55:52 AM Ultima modificación: Abril 11, 2016, 11:36:56 AM por pioj
Buenas gente, acá les traigo un simple script para controlar el movimiento de traslación de las plataformas.
Lo hice con el objetivo de facilitarme este método y dejárselo a los nuevos usuarios del foro.
Es algo muy básico pero cumple su función. El siguiente script se añade a la plataforma que quieras que tenga esta propiedad.
using UnityEngine;
using System.Collections.Generic;
public class PlatformMove : MonoBehaviour
{
    [Range(0.0f, 15.0f)]
    public float speed;
    public enum Path { loop, reverse }
    public Path path;
    public float minDistance = 0.1f;
    public List<Vector3> positions = new List<Vector3>();
    private int index = 0;
    void Start() { ResetPosition(); } //para situar la plataforma al comienzo
    void Update ()
    {
        //Traslacion
        if (index < positions.Count)
        {
            transform.position = Vector3.MoveTowards(transform.position, positions[index], Time.deltaTime * speed);
            if (Vector3.Distance(transform.position, positions[index]) < minDistance) index++;
        }
        else
        {
            //Metodos de traslacion
            if (path == Path.loop) index = 0;
            if (path == Path.reverse)
            {
                positions.Reverse();
                index = 0;
            }
        }
    }
    //EDITOR
    public void AddPosition() { positions.Add(transform.position); }
    public void RemoveLastPosition(){ positions.RemoveAt(positions.Count - 1); }
    public void ResetPosition() { transform.position = positions[0]; }
    public void ResetAll() { positions.Clear(); }
    void OnDrawGizmosSelected()
    {
        if (positions != null)
        {
            for (int i = 0; i < positions.Count; i++)
            {
                if(i < positions.Count - 1)
                {
                    Gizmos.color = Color.green;
                    Gizmos.DrawLine(positions, positions[i + 1]);
                }
                else
                {
                    Gizmos.color = Color.blue;
                    Gizmos.DrawLine(positions, positions[0]);
                }
            }
        }
    }
}
Y el siguiente dentro de la carpeta "Editor"
using UnityEngine;
using Unit***itor;
[CustomEditor(typeof(PlatformMove))]
public class PlatformMoveEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        PlatformMove plataformMove = (PlatformMove)target;
        if (GUILayout.Button("ADD")) plataformMove.AddPosition();
        if (GUILayout.Button("REMOVE LAST POSITION")) { plataformMove.RemoveLastPosition(); }
        if (GUILayout.Button("RESET POSITION (OBJECT)")) plataformMove.ResetPosition();
        if (GUILayout.Button("RESET ALL")) plataformMove.ResetAll();
    }
}
 

Buen aporte, muchas gracias.

Etiquetas: