I have a little problem; I'm trying to develop my own endless jumping game. That's why I created an enemy type that can fire small projectiles. Every time it shoots, a sound should be played, but it won't. Debug: "Can not play disabled audio source."
Originally that only didn't work out for the prefabs, but recently it didn't work out for the first, original opponent either. (I kinda messd up spmething in the code)
Every proposed solution would help me a lot :)
I know, there are some posts about this topic out there, but none of this has helped yet...
The kinda messy code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyingEnemy : MonoBehaviour {
public float startTimeBtwShots;
private float timeBtwShots;
private Vector3 spawnPoint;
public AudioSource shot;
public Animator animator;
private float flyingSpeed;
private float turningPoint;
private bool moving;
public GameObject projectile;
public GameObject player;
void Start ()
{
turningPoint = Mathf.Abs(transform.position.x);
flyingSpeed = 0.008f;
timeBtwShots = startTimeBtwShots;
moving = true;
shot.enabled = true;
}
void Update ()
{
//Ignore, moves enemy from left to right and back
if (moving)
{
transform.position = new Vector3(transform.position.x + flyingSpeed, transform.position.y, transform.position.z);
}
if (transform.position.x >= turningPoint || transform.position.x <= turningPoint * -1)
{
flyingSpeed *= -1;
transform.position = new Vector3(transform.position.x + flyingSpeed, transform.position.y, transform.position.z);
if (flyingSpeed <= 0) transform.eulerAngles = new Vector3(0, 180, 0);
else if (flyingSpeed >= 0) transform.eulerAngles = new Vector3(0, 0, 0);
}
//Important part: projectile gets spawn and a bit earlier the sound should be played...
if (timeBtwShots <= 0)
{
spawnPoint = transform.position;
spawnPoint.y -= 0.35f;
Instantiate(projectile, spawnPoint, Quaternion.identity);
timeBtwShots = startTimeBtwShots;
animator.SetBool("Shooting", false);
}
else
{
timeBtwShots -= Time.deltaTime;
if (timeBtwShots <= 0.3)
{
shot.volume = 1;
shot.Play();
}
if (timeBtwShots <= 0.6)
{
animator.SetBool("Shooting", true);
}
}
if (player.transform.position.y >= transform.position.y + 5.5f)
{
Destroy(gameObject);
}
}
}
Thank you in advance!!
Ego | Jakob
Debug.Log("blah blah")in theifblock where you callPlay, does anything get logged? - Ruzihm