0

Teniendo estos scripts

Bird

using UnityEngine;
using System.Collections;

public class Bird : MonoBehaviour {
    // Movement speed
    public float speed = 1;

    // Flap force
    public float force = 100;

    // Use this for initialization
    void Start () {
        // Fly towards the right
        GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
    }

    // Update is called once per frame
    void Update () {
        // Flap
        if (Input.GetKeyDown(KeyCode.Space))
            GetComponent<Rigidbody2D>().AddForce(Vector2.up * force);
    }

    void OnCollisionEnter2D(Collision2D coll) {
        // Restart
        Application.LoadLevel(Application.loadedLevel);
    }
}

CameraFollow

    using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {
    // The Target
    public Transform target;

    void LateUpdate () {
        transform.position = new Vector3(target.position.x,
                                         transform.position.y,
                                         transform.position.z);
    }
}

Obstacle

using UnityEngine;
using System.Collections;

public class Obstacle : MonoBehaviour {
    // Movement Speed (0 means don't move)
    public float speed = 0;

    // Switch Movement Direction every x seconds
    public float switchTime = 2;

    void Start() {
        // Initial Movement Direction
        GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;

        // Switch every few seconds
        InvokeRepeating("Switch", 0, switchTime);
    }

    void Switch() {
        GetComponent<Rigidbody2D>().velocity *= -1;
    }
}

Como puedo poner fin de mapa?, creaty empty y inserto box collider 2d, y después como introduzco por script que si Bird llega a ese punto gane la partida, supongo que sea como

 void OnCollisionEnter2D(Collision2D coll) {
        // Restart
        Application.LoadLevel(Application.loadedLevel);
    }

pero referenciado al objeto en cuestión

Nuevo Bird con final en tiempo, gracias a la primera respuesta

error en t.Elapsed += T_Elapsed; Assets/Scripts/Bird.cs(21,8): error CS1525: Unexpected symbol `t'

using UnityEngine;
using System.Collections;
using System.Timers;
public class Bird : MonoBehaviour {
    // Movement speed
    public float speed = 1;


    // Flap force
    public float force = 100;

    public double Interval = 10000;

    private Timer t;

    // Use this for initialization
    void Start () {
        // Fly towards the right
        GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
        t = new Timer(Interval)
        t.Elapsed += T_Elapsed;
        t.Start();
    }

    // Update is called once per frame
    void Update () {
        // Flap
        if (Input.GetKeyDown(KeyCode.Space))
            GetComponent<Rigidbody2D>().AddForce(Vector2.up * force);
    }

    void OnCollisionEnter2D(Collision2D coll) {
        // Restart
        t.Stop();
        Application.LoadLevel(Application.loadedLevel);
    }

    private static void T_Elapsed(object sender, ElapsedEventArgs e)
        {
            t.Stop();
            ///
            /// aqui pones el codigo para mostrar que gano
            ///
        }
}

1 respuesta 1

1

Ni siquiera necesitas algo tan complicado, ya que el pájaro avanza hacia adelante a velocidad constante podrías simplemente poner un timer, si el jugador no ha muerto para cuando termine de contar el timer le muestras que ha ganado.

public class Bird : MonoBehaviour {
    // Movement speed
    public float speed = 1;


    // Flap force
    public float force = 100;

    public double Interval = 10000;

    private Timer t;

    // Use this for initialization
    void Start () {
        // Fly towards the right
        GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
        t = new Timer(Interval)
        t.Elapsed += T_Elapsed;
        t.Start();
    }

    // Update is called once per frame
    void Update () {
        // Flap
        if (Input.GetKeyDown(KeyCode.Space))
            GetComponent<Rigidbody2D>().AddForce(Vector2.up * force);
    }

    void OnCollisionEnter2D(Collision2D coll) {
        // Restart
        t.Stop();
        Application.LoadLevel(Application.loadedLevel);
    }

    private static void T_Elapsed(object sender, ElapsedEventArgs e)
        {
            t.Stop();
            ///
            /// aqui pones el codigo para mostrar que gano
            /// 
        }
}
11
  • Como podría hacer eso?
    – kit
    el 31 mar. 2019 a las 13:51
  • ahi actualice la respuesta
    – Bloodday
    el 31 mar. 2019 a las 13:59
  • el intervalo esta en milisegundos, asi que si quires que gane en 5 segundos, le pones 5 mil (si te fijas ahora tiene 10 mil, asi que ganara en 10 segundos
    – Bloodday
    el 31 mar. 2019 a las 14:01
  • Muchas gracias colega :D
    – kit
    el 31 mar. 2019 a las 16:34
  • Si me permites la última pregunta, como puedo poner ese código para mostrar que ganó?
    – kit
    el 31 mar. 2019 a las 16:38

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.