Noticias

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

Rebobinar-volver al inicio una animación.

Iniciado por ramunda, Agosto 22, 2016, 03:10:35 PM

Tema anterior - Siguiente tema
Agosto 22, 2016, 03:10:35 PM Ultima modificación: Agosto 22, 2016, 03:14:36 PM por ramunda
Buenas tardes,
 
tengo un pequeño dilema que me lleva de cabeza, he buscado y remirado pero nada da en el clavo.
 
Tengo una puerta que se abre con una animación con un trigger, bien la animación abre la puerta ,
 
El caso es que quiero que se cierre la puerta cuando por ejemplo me matan o lo que sea.En este caso en cuando pierdo una bola.
 
Pues no hay manera. He probado con rewind, con -1 y +1 de todo ..la puerta no se cierra.
 
Os enseño como lo tengo aver si teneis alguna idea.
 
este la abre:
 

using UnityEngine;
using System.Collections;
public class x3 : MonoBehaviour {
public float force = 100.0f;
public float forceRadius = 1.0f;
public int scoreValue=100000;
public GameObject spotlight;
public AudioClip bumpersound;
public AudioClip puerta;
AudioSource audio;
public GameObject puerta3;
public GameObject luzx1;
public  GameObject TextOnSpotGO;
public GameObject light;
Animation anim;
// public GameObject puntacos;
void Start ()
{
   audio = GetComponent<AudioSource>();
   anim =puerta3.GetComponent<Animation> ();
   light.SetActive (false);
   spotlight.SetActive (false);
}
void OnTriggerEnter (Collider col)
{
   ScoreManager.Instance.SetScorePower (2);
   if(col.gameObject.name == "Ball")
   Instantiate (TextOnSpotGO, transform.position, Quaternion.identity);
   TextOnSpotGO.GetComponent<TextOnSpotScript>().DisplayText = ScoreManager.Instance.scoreagujeros * ScoreManager.Instance.scorePower + "";
   ScoreManager.Instance.ScoreUp(scoreValue);
   anim.Play ("Take001");
   Invoke ("apagado", 03);
      
}
void apagado()
{
   spotlight.SetActive(true);
   audio.PlayOneShot(bumpersound, 0.9F);
   light.SetActive (true);
   luzx1.SetActive (false);
   spotlight.SetActive (true);
}
}

 
Este debería cerrarla al perder una bola:
 

using UnityEngine;
using System.Collections;
public class BallManager : MonoBehaviour
{
public GameObject ball;
public Transform spawnPosition;
public TextMesh display;
public int lives = 3;
public bool gameOver = false;
AudioSource audio;
public GameObject luzx1;
public GameObject luzx2;
//public GameObject puerta1;
//public GameObject puerta2;
public GameObject puerta3;
Animation anim;
 
void Start()
{
   
   anim = GetComponent<Animation>();
   AudioSource audio = GetComponent<AudioSource> ();
}
   
void Update()
{
   //AudioSource audio = GetComponent<AudioSource> ();
   //Check for gameover
   if (lives < ) {
      if (display) {
         display.text = "GAME OVER";
         gameOver = true;
      }
   }
   
   //Ball cae y no es game over
   if (!GameObject.FindGameObjectWithTag ("Ball") && !gameOver) {
      lives--;
      Instantiate (ball, spawnPosition.position, ball.transform.rotation);
      audio.Play ();
      luzx1.SetActive (true);
      luzx2.SetActive (false);
      puerta3.GetComponent<Animation>() ["Take 001"].speed = -1;
      puerta3.GetComponent<Animation> () ["Take 001"].time = GetComponent<Animation> () ["Take 001"].length;
      puerta3.GetComponent<Animation> ().Play ("Take 001");
   

      //Update bolas restantes
      if(display)
      {
         display.text = "BOLAS RESTANTES: " + lives.ToString();
      }
   }
   
   //Reset game with return key if game is over
   if(gameOver)
   {
      if(Input.GetKeyDown(KeyCode.Return))
      {
         Application.LoadLevel(Application.loadedLevel);
      }
   
   }
}
}

 
Gracias de antemano.
 
 
 
 

Puedes hacerlo iniciando la animación con Play desde el inicio con una velocidad 0(en pausa). Luego asignarías la posición de la animación manualmente mediante normalizedTime:
 

using UnityEngine;
using System.Collections;
public class PuertaControl : MonoBehaviour {
    public Animation animPuerta;
    bool abrir = false;
    void Start()
    {
        this.animPuerta["abrir"].speed = 0f;
        this.animPuerta.Play("abrir");
    }
void OnTriggerEnter(Collider collider)
    {
 
        if (collider.tag.Equals("Player")) { this.abrir = true; }
    }
    void OnTriggerExit(Collider collider)
    {
 
        if (collider.tag.Equals("Player")) { this.abrir = false; }
    }
    void Update()
    {
        if (this.abrir)
        {
            if(this.animPuerta["abrir"].normalizedTime < 1f)
            {
                float time = this.animPuerta["abrir"].normalizedTime + Time.deltaTime;              
                this.animPuerta["abrir"].normalizedTime = Mathf.Min(1f, time);            
            }
           
        } else
        {
            if (this.animPuerta["abrir"].normalizedTime > 0f)
            {
                float time = this.animPuerta["abrir"].normalizedTime - Time.deltaTime;
                this.animPuerta["abrir"].normalizedTime = Mathf.Max(0f, time);
            }
 
        }
    }
}

 
 
 
También podrías hacerlo solo controlando la velocidad de la animación, evitando que llegue al final(normalizedTime=1), pausando la animación en el tiempo 0.9. Tendrías que tenerlo en cuenta en el momento de crear la animación:
 

using UnityEngine;
using System.Collections;
public class PuertaControl : MonoBehaviour {
    public Animation animPuerta;
    bool abrir = false;
 
void OnTriggerEnter(Collider collider)
    {
 
        if (collider.tag.Equals("Player")) { this.abrir = true; }
    }
    void OnTriggerExit(Collider collider)
    {
 
        if (collider.tag.Equals("Player")) { this.abrir = false; }
    }
    void Update()
    {      
        if (this.abrir)
        {
            if(this.animPuerta["abrir"].normalizedTime < 0.9f) this.animPuerta["abrir"].speed = 1f;
            else this.animPuerta["abrir"].speed = 0f;
                       
        }
        else
        {
            if (this.animPuerta["abrir"].normalizedTime > 0f) this.animPuerta["abrir"].speed = -1f;
            else this.animPuerta["abrir"].speed = 0f;
            if (!this.animPuerta.isPlaying) this.animPuerta.Play("abrir");
        }    
    }
}

 
 
 
Enlace de descarga: http://www.speedyshare.com/kr5vB/proyecto.unitypackage

Kaito me has dejado sin palabras las 2 maneras son excelentes, mil gracias!!!
 
 
 
 

Etiquetas: