using UnityEngine;
using System.Collections;
public class PlayerInfo : MonoBehaviour {
public GameObject healthBottle;
public GameObject player;
public AudioClip deathClip;
public AudioClip deathClip2;
public float length;
int health = 100;
int damageCount = 0;
int currentHealth;
bool playerFighting;
public GUIText healthbar;
float timer = 0.0f;
float healthTimer = 0.0f;
bool healthShouldRespawn = false;
// Use this for initialization
void Start () {
currentHealth = health;
}
// Update is called once per frame
void Update () {
Cursor.visible = false;
print("player in combat? " + playerFighting);
if(!playerFighting){
print("player is not fighting");
if(currentHealth != 100){
timer += Time.deltaTime;
print ("timer: " + timer);
if(timer > 90.0f){
currentHealth += 10;
timer = 0.0f;
}
}
}
else
{
if(currentHealth <= 0){
GetComponent<AudioSource>().PlayOneShot(deathClip);length = 0.8f;
transform.position = new Vector3(Random.Range(50.0f, 120.0f), 0, Random.Range(10.0f, 200.0f));
currentHealth = 100;
}
}
healthbar.text = "Vida " + currentHealth + " / 100";
if(healthShouldRespawn)
{
healthTimer += Time.deltaTime;
if(healthTimer > 5.0f)
{
healthBottle.gameObject.SetActive(true);
healthTimer = 0.0f;
}
}
}
public void ApplyDamage(int damage){
damageCount = damageCount + damage;
if(damageCount == 100){
currentHealth = currentHealth - 30;
GetComponent<AudioSource>().PlayOneShot(deathClip2);length = 0.5f;
healthbar.text = "Health " + currentHealth + " / 100";
damageCount = 0;
}
print(currentHealth);
}
public void playerInCombat(int combat){
if(combat == 1){
playerFighting = true;
}
else
{
playerFighting = false;
}
}
void OnTriggerEnter(Collider item){
if(item.gameObject.tag == "PlayerHealthBottle")
{
if(currentHealth < 100)
{
currentHealth = currentHealth + 50;
item.gameObject.SetActive(false);
healthShouldRespawn = true;
}
}
}
}
f(currentHealth <= 0){
GetComponent<AudioSource>().PlayOneShot(deathClip);length = 0.8f;
if(damageCount == 100){
currentHealth = currentHealth - 30;
GetComponent<AudioSource>().PlayOneShot(deathClip2);length = 0.5f;
healthbar.text = "Health " + currentHealth + " / 100";
damageCount = 0;
Cita de: ramunda date=1435587886Hola muy buenas tengo un dilema, tengo un sonido que cuando le pegan a mi pelele grita, y si muere suena otro grito.Nadie me puede echar un cable?
Eso lo hace, pero no suena como deberia suena como si estuviera a 1000 revoluciones por minuto osea super rapido o agudo por decir algo , se me ocurrio ponerle un length para determinarle el tiempo de durada de este audioclip..pero me hace caso omiso sigue rapido y no suena bien.
Alguna idea?
aqui el script:
using UnityEngine;
using System.Collections;
public class PlayerInfo : MonoBehaviour {
public GameObject healthBottle;
public GameObject player;
public AudioClip deathClip;
public AudioClip deathClip2;
public float length;
int health = 100;
int damageCount = 0;
int currentHealth;
bool playerFighting;
public GUIText healthbar;
float timer = 0.0f;
float healthTimer = 0.0f;
bool healthShouldRespawn = false;
// Use this for initialization
void Start () {
currentHealth = health;
}
// Update is called once per frame
void Update () {
Cursor.visible = false;
print("player in combat? " + playerFighting);
if(!playerFighting){
print("player is not fighting");
if(currentHealth != 100){
timer += Time.deltaTime;
print ("timer: " + timer);
if(timer > 90.0f){
currentHealth += 10;
timer = 0.0f;
}
}
}
else
{
if(currentHealth <= 0){
GetComponent<AudioSource>().PlayOneShot(deathClip);length = 0.8f;
transform.position = new Vector3(Random.Range(50.0f, 120.0f), 0, Random.Range(10.0f, 200.0f));
currentHealth = 100;
}
}
healthbar.text = "Vida " + currentHealth + " / 100";
if(healthShouldRespawn)
{
healthTimer += Time.deltaTime;
if(healthTimer > 5.0f)
{
healthBottle.gameObject.SetActive(true);
healthTimer = 0.0f;
}
}
}
public void ApplyDamage(int damage){
damageCount = damageCount + damage;
if(damageCount == 100){
currentHealth = currentHealth - 30;
GetComponent<AudioSource>().PlayOneShot(deathClip2);length = 0.5f;
healthbar.text = "Health " + currentHealth + " / 100";
damageCount = 0;
}
print(currentHealth);
}
public void playerInCombat(int combat){
if(combat == 1){
playerFighting = true;
}
else
{
playerFighting = false;
}
}
void OnTriggerEnter(Collider item){
if(item.gameObject.tag == "PlayerHealthBottle")
{
if(currentHealth < 100)
{
currentHealth = currentHealth + 50;
item.gameObject.SetActive(false);
healthShouldRespawn = true;
}
}
}
}
Esta es la parte de donde muere:
f(currentHealth <= 0){
GetComponent<AudioSource>().PlayOneShot(deathClip);length = 0.8f;
y este fragmento es el que cuando le pegan grita:
if(damageCount == 100){
currentHealth = currentHealth - 30;
GetComponent<AudioSource>().PlayOneShot(deathClip2);length = 0.5f;
healthbar.text = "Health " + currentHealth + " / 100";
damageCount = 0;
Las dos partes constan de su length que esta arriba con su public float correspondiente....
Cita de: kalua66 date=1435743564Probaste en el AudioSource a poner el:Pitch:Amount of change in pitch due to slowdown/speed up of the Audio Clip. Value 1 is normal playback speed.Kalua66, gracias por la respuesta, el caso es que no tengo audio source puesto ya que lo llamo directamente desde aqui:
Pitch => 1
y arrastro directamente el sonido deseado a la casilla que me pide.
igualmente he pobado añadiendo el audiosource y bajandole el pitch como me has dicho y el efecto es el mismo...no fuanciona, a veces se reproduce bien aveces acelerado aveces entrecortado...no se que hacer
GetComponent<AudioSource>().
Cita de: kalua66 date=1435752784Seguro que no tienes ningun audioSource ?Sisi seguro que no tenia udiosource..ahora se lo puse por tus indicaciones y sigue igual y si, el sonido por si solo ejecutandolo desde unity suena bien.....
lo digo por estas lineas que pusiste..
GetComponent<AudioSource>().
Mira ver que igual esta modificando el de otro lado...
El sonido si lo pruebas desde los ajustes de importación ,suena bien ?
Sino es eso ni idea..
GetComponent<AudioSource>()
Cita de: kalua66 date=1435753440Pues esta linea que tienes en tu script:Te juro por snoopy que no los tenia y no me daba error..ahora la he liado un poco mas!! , he modificado y puesto esto:
GetComponent<AudioSource>()
lo que hace es coger el componente AudioSource, y sino lo tenias tenia que haber dado error... no has comentado nada de ningún error y dices que suena, por lo tanto tienes que tenerlo, por narices...
if(currentHealth <= 0){
GetComponent<AudioSource>();//// aqui midificado
//yield return new WaitForSeconds (5);// esta linea es la de pausa y la que da problemas
transform.position = new Vector3(Random.Range(50.0f, 120.0f), 0, Random.Range(10.0f, 200.0f));
currentHealth = 100;
}
Cita de: kalua66 date=1435759524Buff...tienes mucho lio montado, te recomendaria que vieras tutoriales primero, y que entiendas que Unity trabaja por componentes..y como se accden a ellos y demas.. te podria hacer el codigo, pero no serviria de nada, creeme..