Noticias

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

(SOLUCIONADO) ¿Como hacer que la camara tiemble despues de una colision?

Iniciado por ftejada, Enero 21, 2016, 01:31:47 PM

Tema anterior - Siguiente tema
Hola a todos... ¿Como podría conseguir un buen efecto para que la camara tiemble? He estado mirando y con las formas de hacerlo via script que he encontrado no me gustan los resultados.He probado a hacer una animacion a la camara y el resultado es mucho mejor, pero tengo el problema de que mi camara se mueve via script persiguiendo al player y cuando ejecuto la animacion, la camara la hace enfocando al lugar original que enfocaba  cuando hice la animacion en el Animation... Es decir que enfoca a un sitio que no tiene nada que ver con donde está el player... Existe la manera de poder hacer que esa animacion de temblar creada en el Animation, se pueda utlizar mientras la camara se mueve via script persiguiendo al player ,sin que deje de enfocarlo ??? Saludos y gracias...

Prueba este codigo: 
using UnityEngine;
using System.Collections;

public class Shake : MonoBehaviour {

public CameraShake cameraShake;

void Update () {

   if(Input.GetKeyDown(KeyCode.Space)){

      StartCoroutine(this.cameraShake.Shake(Camera.main));

   }

}

}


[System.Serializable]
public class CameraShake{

public Vector3 Range;
public Vector2 pauseTime;
public int Loop;

public IEnumerator Shake(Camera camera){

   Quaternion rotation = camera.transform.rotation;

   for(int i=0;i<this.Loop;i++){

      camera.transform.rotation = rotation * Quaternion.Euler(Random.Range(-this.Range.x,this.Range.x),
                                                              Random.Range(-this.Range.y,this.Range.y),
                                                              Random.Range(-this.Range.z,this.Range.z));

      yield return new WaitForSeconds(Random.Range(this.pauseTime.x,this.pauseTime.y));

   }

   camera.transform.rotation = rotation;

}

}
 Yo lo he probado con un Range = 5,5,0 y un pauseTime 0.05,0.3 y va bastante bien.

[quote author=kaito date=1453584368" data-ipsquote="" data-cite="kaito" class="ipsQuote">Prueba este codigo: 
using UnityEngine;using System.Collections;

public class Shake : MonoBehaviour {

public CameraShake cameraShake;

void Update () {

   if(Input.GetKeyDown(KeyCode.Space)){

      StartCoroutine(this.cameraShake.Shake(Camera.main));

   }

}

}


[System.Serializable]
public class CameraShake{

public Vector3 Range;
public Vector2 pauseTime;
public int Loop;

public IEnumerator Shake(Camera camera){

   Quaternion rotation = camera.transform.rotation;

   for(int i=0;i<this.Loop;i++){

      camera.transform.rotation = rotation * Quaternion.Euler(Random.Range(-this.Range.x,this.Range.x),
                                                              Random.Range(-this.Range.y,this.Range.y),
                                                              Random.Range(-this.Range.z,this.Range.z));

      yield return new WaitForSeconds(Random.Range(this.pauseTime.x,this.pauseTime.y));

   }

   camera.transform.rotation = rotation;

}

}
 Yo lo he probado con un Range = 5,5,0 y un pauseTime 0.05,0.3 y va bastante bien.[/quote]Gracias por responder kaito. Voy a probarlo en un par de días y comento a ver como me ha ido. Saludos

Ya he probado eso kaito. Me va bastante bien pero dejando a 0 los valores pauseTime. Gracias por el aporte . Un saludo

Ahí va otro Shake, este no se de donde lo pille xDDDD
using UnityEngine;

public class ZmbaCam : MonoBehaviour
{
public float shakeFrequency = 0.2f;
public float shakeLerp = 0.9f;
public Vector3 shakePower = Vector3.one;
private Transform thisTransform;
private Vector3 originalPosition;
public bool Agitame;
private float nextShakeTime;
private Vector3 shakeTo;
private float shakeDuration;
private float shakeDurationElapsed;
private float shakeStrength;
private float shakeStrengthElapsed;

private void Awake()
{
   this.thisTransform = this.transform;
   this.originalPosition = this.thisTransform.localPosition;
   this.shakeTo = this.originalPosition;
   this.shakeDuration = 0.0f;
}

public void Shake(float strength, float duration)
{
   if ((double) strength <= (double) this.shakeStrengthElapsed)
      return;
   this.shakeStrength = this.shakeStrengthElapsed = strength;
   this.shakeDuration = this.shakeDurationElapsed = duration;
}

private void Update()
{
   if (this.Agitame)
   {
      this.shakeStrength = this.shakeStrengthElapsed = 1f;
      this.shakeDuration = this.shakeDurationElapsed = 1f;
   }
   if ((double) this.shakeDurationElapsed > 0.0)
   {
      this.shakeDurationElapsed -= Time.deltaTime;
      if ((double) this.shakeDurationElapsed <= 0.0)
      {
         this.shakeDurationElapsed = 0.0f;
      }
      else
      {
         this.nextShakeTime -= Time.deltaTime;
         this.shakeStrengthElapsed = this.shakeStrength * (this.shakeDurationElapsed / this.shakeDuration);
         if ((double) this.nextShakeTime <= 0.0)
         {
            this.nextShakeTime = this.shakeFrequency;
            this.shakeTo = this.originalPosition + new Vector3(Random.Range(-1f, 1f) * this.shakePower.x, Random.Range(-1f, 1f) * this.shakePower.y, Random.Range(-1f, 1f) * this.shakePower.z) * this.shakeStrengthElapsed;
         }
         this.thisTransform.localPosition = Vector3.Lerp(this.thisTransform.localPosition, this.shakeTo, Time.deltaTime * this.shakeLerp);
      }
   }
   else
   {
      if (!(this.thisTransform.position != this.originalPosition))
         return;
      this.thisTransform.localPosition = Vector3.Lerp(this.thisTransform.localPosition, this.originalPosition, Time.deltaTime);
   }
}
}
 

Etiquetas: