In my game I am creating in unity I am trying to play an audio clip every time that a collision happens on one of my game objects. For some reason the audio will play the first time there is a collisiom, but from then on it no longer plaus the sound.
The game object that is collided with has a component for the AudioSource and I have the audio clip selected in that component.
Here is the code where I start the audio:
if (PlayerPrefs.GetInt ("Sound Playing", 1) == 1)
{
audio = GetComponent <AudioSource>();
audio.Play ();
}
I have also tried using the PlayOneShot () method but it does the same thing.
EDIT
Here is a small representation what of my game is doing here:
public class Ship : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collider)
{
Laser laser = collider.gameObject.GetComponent<Laser> ();
if (laser)
{
if (!immune)
{
//update healthbar
healthbar.hit();
//reset health boost
forcefield.resetHealthBoost();
health -= laser.getDamage(); //remove health from ship
//create explosion
GameObject explosion = Instantiate(explode, gameObject.transform.position, Quaternion.identity) as GameObject;
explosion.GetComponent<ParticleSystem>().startColor = this.GetComponent<SpriteRenderer>().color;
if (SoundEffects.soundOn && PlayerPrefs.GetInt("Sound Playing", 1) == 1)
{
audio = GetComponent<AudioSource>();
audio.Play();
}
}
laser.hit();//destroy the laser that hit the ship
}
}
}
Here is to show that I have the AudioSource and the AudioClip setup in the inspector (the AudioSource is attached to the ship):
The issue with my game was that I didn't realize that using gameobject.SetActive(false) later in my code was stopping the second audio clip from being able to play. I set it to false when the ship was destroyed and this is what the problem was all along. It works fine now.
It's looks with audio it's all right. May be problem is in collisions or something else. Add code Debug.Log("Play sound."); before code line audio.Play();, and test.
Make sure your audio listener is near the explosion too. It could be far away and may not be heard. Also do a check to see if audio.isPlaying, then play audio. Audio may be still playing and you have to stop it properly.
if (audio.isPlaying) {
audio.Stop();
audio.Play();
}
If all else fails you can always do PlayOneShot(), which will essentially do as it says. audio.PlayOneShot(); This could be problematic though since if you are putting it OnTriggerEnter() it may run more than once in a short duration. You may hear it stutter because it's queued up however many explosions.
Finally, don't use PlayerPrefs for those specific things. You should reserve it honestly for Game Specific data storage. Why can't you simply use isPlaying? PlayerPrefs will only make the task very convoluted. I learned it the hard way when I used to use PlayerPrefs to transfer data from C# to UnityScript. In the end it's almost best to use Static Variables, or create a Singleton class and commit values to various instances. Plus I learned that I can use methods and variables cross languages when I adjust the compilation order(s). A little bit off topic, but still substantially important.
Related
I have animation events linked up to my characters that play a random footstep noise when they step. I'm trying to set it up for multiplayer, but I'm having some issues. With one person, the sounds only play once when they're supposed to. However, as tested with 2 people, it plays each footstep twice at the same time when one player steps. Each player has an audiosource component. Both footsteps sounds come from the audiosource of the player running, so it's not a case of both players playing the same sound. Any ideas as to why the sound is duped and played at the same time? The double sound comes from the same client, but only when that client is in multiplayer. And it's not when other people are walking, only the client. I must be setting up something wrong or putting something in the wrong place with my RPC.
1 player with 1 audiosource: sounds plays once
2 players with their own audiosource: sounds duplicates and plays at the same time
2 players with audiosource enabled for only the one walking: sounds still plays twice
From my player code
public void PlayFootstep()
{
int clipPick = Random.Range(0, footstepArray.Length);
GetComponent<AudioSource>().clip = footstepArray[clipPick];
photonView.RPC("PlayFootstepRPC", RpcTarget.All);
}
[PunRPC]
private void PlayFootstepRPC()
{
if (GetComponent<AudioSource>().isActiveAndEnabled && GetComponent<PlayerMovement>().ySpeed > 1.15)
{
GetComponent<AudioSource>().Play();
}
}
If PlayFootstep is called via an animation event, and you have animations synchronized via PhotonAnimatorView, then the PlayFootstepRPC() gets called several times, once per each connected client.
PhotonAnimatorView makes an object to play the same animations on every client. The PlayFootstep function gets called on every client, and every client sends RPC to itself and other clients, and that RPC plays the sound.
I suggest you should either not play footstep sounds via RPC, playing it locally instead (because animation event handles it for you), or add a check of PhotonView.IsMine before calling an RPC.
I’m trying to find a good way to play background music in Unity 3D. I want the music to keep playing consistently through scene loads. Don’t Destroy on load is fine and works, but every time I load the same scene, it makes another music game object because the scene itself has the game object in it. How can I solve my problem? I am a “beginner” (kind of), so I would like code I can understand.
I'd hands down recommend starting with an Asset like 'EazySoundManagerDemo'. It needs a little refactoring and refinement (ie it uses 3 arrays of audios with 3 sets of accessibility functions instead of one set with an AudioPurpose enum to increase code-reuse).
It does however solve the basic problem you have and is a good intro to using an audio manager / layer instead of simply playing audio directly from your GameObjects. Give that a shot, learn from it and then adapt it or create your own audio management layer.
Good Luck!
I recommend creating an audioSource object, then creating an script for this object and on the awake function do this:
void Awake() {
DontDestroyOnLoad(this.gameObject);
}
This will make the background music to keep playing between scenes. For more information you could use Unity's documentation about this function.
With help from a question on the unity forum, I think I have solved my problem. The link to the question is here...
https://answers.unity.com/questions/982403/how-to-not-duplicate-game-objects-on-dontdestroyon.html
The Best Answer is the one I’m using.
The code is this...
private static Player playerInstance;
void Awake(){
DontDestroyOnLoad(this);
if (playerInstance == null) {
playerInstance = this;
} else {
Destroy(gameObject); // Used Destroy instead of DestroyObject
}
}
I have a terrain with two enemies that I placed and a plane that I have given a script to spawn enemies continuously (basically wave defence without towers)
The two enemies I placed work as intended, killing them will give experience and coming into contact with them cost the player some health.
But the ones spawned by my scripted plane don't grant experience on a kill, the only reason they do health damage on contact is because I set the collision to look for a tag of "Enemy" which they have.
# THE SCRIPT FOR THE ENEMY-SPAWNING PLANE
IEnumerator Spawner()
{
yield return new WaitForSeconds(startWait);
while(!stop)
{
randEnemy = Random.Range(0, 2);
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 2, Random.Range(-spawnValues.z, spawnValues.z));
Instantiate(enemies[randEnemy], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds(spawnWait);
}
}
# THE SCRIPT WHICH CALLS FOR EXPERIENCE TO BE GIVEN (Enemy as GameObject)
void Dead()
{
_characterXP.GainExp(120);
Destroy(gameObject);
}
# THE SCRIPT WHICH GIVES THE PLAYER EXPERIENCE (CharacterXP as GameObject)
public void GainExp(float expThatWasGained)
{
CurrentExp += expThatWasGained;
}
I'm still pretty new to Unity and C# so if there is anything else that's needed I can provide it, I have checked all relevant objects to make sure the relevant things are connected correctly, and if they didn't work then it would affect the original enemies, the issue is only in regards to the spawned enemies that get named ENEMY(Clone)
EDIT
The Prefab ^
The Pre-Spawned Enemy Mid-Game^
The Clone Enemy Mid-Game^
You cannot assign scene objects to prefab property fields. You can only assign other prefab (or assets) into the prefab property slots.
To assign a scene object to your enemy script, you need to find it at runtime.
In your enemy's Start method, find the fpscontroller
void Start()
{
_characterXP = GameObject.FindObjectOfType<FPSController>();
}
I think this will solve your problem
Not the most intelligent way of doing this, but I was on a tight schedule and the method works.
I created two prisons outside of the playable area and put one of each enemy inside of it and then referred to these two trapped individuals in my Monster Spawner (The Plane) instead of the prefabs and now I gain the right amount of exp per kill.
I'm making/want to make a bullet-hell game where there are essentially two players, each own their own identical copy of a map competition for the high score. And the game ends whenever a player dies.
I have all of this working in multiplayer, but my approach seems a bit hacky, so I'm wondering if there's a better way for me to accomplish this.
At first I was using just unity's network manager, and in my player Start() function I checked if(!isLocal), and if so I set the gameObject enabled to false. This worked great, but like I said, felt a little hacky.
Example of the code:
if (!isLocalPlayer) {
gameObject.SetActive(false);
return;
}
Next I moved to unity's LobbyManager. This is where things got really sticky. Now on the Host, the game loads fine, but on the client, only one game object is created, and it's set as disabled which leads me to believe that it is the 'enemy' or not local player object.
I slowly figured out that the cause of this was setting the enemy game object active to false. If I left it true, both players would spawn on both screens. My solution now is to not disable the enemy player object, but every component on it so it doesn't get in the way.
Again this feels very hacky, and like it could lead to problems down the road. Is this really the best option, or am I missing something obvious?
Example of the Code:
if (!isLocalPlayer) {
gameObject.GetComponent<MeshRenderer>().enabled = false;
gameObject.GetComponent<BoxCollider>().enabled = false;
gameObject.GetComponent<CharacterController>().enabled = false;
gameObject.GetComponent<PlayerController>().enabled = false;
gameObject.GetComponent<Rigidbody>().detectCollisions = false;
guns = gameObject.GetComponentsInChildren<MeshRenderer>();
foreach (MeshRenderer gun in guns) {
gun.enabled = false;
}
}
Thanks in advance! Sorry this is so long, hopefully it isn't a chore to read.
Make an empty scene with nothing but a score manager and what else you need to transfer.
Have another scene as an asset in your project folder, this is where the map is.
When a player joins, have them join the scene with the score manager.
If they also are the local player, they should load the map scene.
That way, both players will be in the same scene while also having their own instance of the map.
You can load scenes in asynchronously and additively, which would be ideal for your situation.
I have an issue that I've not been able to solve for a few days now and without it working I can't move on with my project, so your help would be greatly appreciated!
What I'm trying to do is 'find' the velocity of an object that is directly below my 2D sprite (that also contains a Rigidbody and 2D box collider) and then add that velocity (in the same direction) to the object that is 'looking' for it.
I feel like ray-casting might be part of the answer but I'm not sure how to use that, especially in this context.
The idea behind this is I have a platform that can carry objects stacked on top of each other, so you move the mouse, it manipulates the platforms velocity, but this causes the objects on-top to fly backwards, no mater how high the friction is.
Here is the platform code:
void Update()
{
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
mouseDelta = pos_move - lastMousePosition;
acceleration = mouseDelta.x * 0.4f;
platformVelocity += acceleration;
platform.velocity = new Vector2(platformVelocity, 0) * speed;
lastMousePosition = pos_move;
}
Thank you for taking the time to read this, I would very much appreciate any help you can give!
Is it necessary to 'find' that object? 'Finding' may be difficult & costing.
Can you hold the 'below-thing' reference and pass it to 'above-thing'?
I'm assuming you have some method on objects that checks whether it's on a moving platform. One option is to add an IsOnMovingPlatform flag on those objects. Then use a similar method to check if any object is on top of the "IsOnMovingPlatform == true" objects. Keep doing passes on that method until you don't come up with any more flags set to true.
So, i'm trying to understand your question and I think I might know what you're talking about.
Are you saying that you want your game object (2d sprite) to stay on the platform as its moving? I have had the same problem, where a platform is moving and my player (game object) slides off- and this is how I fixed it.
Add a box collider to the platform (trigger). When the player enters the platform, make the player's transform the platform. That way, when the platform moves- the player moves with it no matter what the speed is.
For example:
void OnTriggerEnter2D(Collider2d collider){
if(collider.tag.equals("Player")){ // you only want the player to work
// attach script to platform for "this.transform" to work
collider.transform.SetParent(this.transform);
}
}
then, to unparent the transform "otherwise, even when the player is off the platform, it will move with the platform.
void OnTriggerExit(Collider2d collider){
// assuming your player isn't supposed to have a parent!
collider.transform.parent = null;
}
Sorry if I misunderstood your question! If this is what you were going for, finding the velocity of the platform is not necessary. Also, this is uncompiled code- if you have issues let me know.
EDIT:
To add a bit of a give, for the entire duration that the player is in contact with the platform, we can add a little bit of a negative force to shove the player a little bit!
We do this by using OnTriggerStay2D (assuming your game is 2d, otherwise use OnTriggerStay).
For example:
void OnTriggerStay2D(Collider2d collider){
if(collider.tag.equals("Player")){
collider.getComponent<Rigidbody2D>().addForce(new vector2(forceToAdd, 0));
}
}
now, that "forceToAdd" variable should be based on the speed and direction that your platform is going. Does your platform have a rigid body on it? if so you can get your force variable by retrieving the rigid body from the platform (getcomponent()) and get the X velocity from it. With this velocity, experiment with dividing it by numbers until you find a good fit, multiply it by -1 (because you want to push the player in the opposite direction of the platform travel), then this will be your forceToAdd!
However, if your platform is not using a Rigidbody, then a way you can do this is record the x positions in your script (just two positions, and subtract these to find the distance in an update method), multiply by -1, try dividing by a number (experiment with this) and that can be your force to add variable.
Also, my code for the OnTriggerStay2d is rough, the spelling could be a bit wrong- but this will give you a rough idea for a solution! ALSO.. I don't think getting the component of a rigid body should be done in every frame, a way around this would to have your rigid body component as a global variable in the script, then set your rigid body on OnTriggerEnter2d, this way it will only retrieve it once (instead of every frame) which I assume would be more efficient!
Good luck! and if you have any questions il try to help!
This is a raycast2d version, I tested it and it works much better than the trigger collider version :).
using UnityEngine;
using System.Collections;
public class RaycastFeetDeleteLater : MonoBehaviour {
[SerializeField] private float raycastDist = 0.1f;
[SerializeField] Transform feet;
[SerializeField] private float xForceDampener = 0.1f;
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void FixedUpdate () {
RaycastDown ();
}
private void RaycastDown(){
RaycastHit2D hit = Physics2D.Raycast (feet.position, Vector2.down, raycastDist);
if (hit.collider != null) {
print (hit.transform.tag);
if (hit.collider.tag.Equals ("Platform")) {
transform.SetParent (hit.transform);
Vector2 force = new Vector2 ((hit.transform.gameObject.GetComponent<Rigidbody2D> ().velocity.x * xForceDampener) * -1, 0);
print (force);
rb2d.AddForce (force);
} else if (transform.parent != null) {
print ("UNPARENT");
transform.parent = null;
}
} else {
if (transform.parent != null) {
transform.parent = null;
}
}
Debug.DrawRay (feet.position, Vector3.down, Color.green, raycastDist);
}
}
This script is attached to the player! No need to have scripts on the platforms, which was the main reason my previous method was a pain :P
Basically this script does the following:
- Gets rigidbody2d of the player on start (we will be adding forces to the player with it soon, so might as well store it in a variable!
On FixedUpdate (Different than Update, FixedUpdate is called on Physics update, where as Update is called every frame) I have read that raycast should use FixedUpdate, I suppose because it is a Physics method!
RaycastDown shoots a 2D raycast directly down from the feet position (to get the feet, create a child object of the player, put the position of the feet a tiny bit below the player, NOT ON THE PLAYER- I will explain why in a second why that will mess up everything! Then drag the feet GameObject on to the "feet" slot of the script.)
Raycast shoots from feet directly down at a distance that is changeable in the editor (raycastDist) I found a distance of 0.1f was perfect for my needs, but play around with it for yours! You can actually see a drawing of your Ray by using Debug.DrawRay as seen in the code, the ray is only visible in the editor- not game view! But this will help, especially with positioning the feet.
Checks if it hit an object with a collider, if so check if the objects tag is a Platform (set the platforms to a new tag and call it Platform).
If it hits a platform set the parent of the player to the platform (like the last script) and start applying a little force to the player in the negative direction that the platform is traveling (we get the rigid body of the platform to find its velocity).
If the raycast does not hit a platform, unparent it so the player can move freely.
Thats basically it, however...
Please note:
This script needs to be on the player, not any of the players child objects, since this script will unparent the player from any platforms- if you have the script on a child object of the player it will unparent from the player- which is horrible. So The script MUST be on the root game object of the player. There are ways around this if for some reason you can't.
As mentioned before, the "feet" child object must be a little bit under the player- this is because when the Raycast2d "fires" it will find the FIRST object with a collider it hits- so if you have the start of the raycast hit a collider on the player then it will always show the raycast hitting the player, not a platform. So to get around this have the raycast fire from the feet- and have the feet position a tiny bit below the player. To help with this I added a print statement that will print to the console which object the raycast is hitting, if it doesn't say "Platform" then you need to lower the feet! :)
Furthermore, I'm not sure how you are moving your platforms- but if the Velocity of the platform always prints "0,0" then its because you are moving the platforms position directly, instead of moving it by forces (this happened to me when I was testing this script). I got around this by moving the platform by "AddForce". Also depending on the "Mass" of your platforms, you may need to drastically increase the "xForceDampener" which I should of actually labeled "xForceMultiplier" as I found while testing I needed to set it much higher- like at 60, instead of 0.25 which I previously thought would be okay..
Hope this helped :D
EDIT: Linking the unity docs for 2D Raycasts in case you need
http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html