Unity3D: Setting AudioSource volume dynamically using AnimationCurve - unity3d

Newcomer to Unity here. For this project, I am reading in an outside AnimationCurve, and attempting to use it to adjust the volume of my GameObject every frame in the Update function. I have found documentation on how to access the volume of my AudioSource GameObject component (this is working), but I can't figure out how to use my evaluated curve to update the volume.
Screencap with imported AnimationCurve and evaluated values
My cs script is included below. Sorry if it's a bit opaque. This is for a neuroscience experiment, so I need to carefully control the animation curves. It reads in times and values (stored in currentCurveData) used to add keyframes to the new AnimationCurve (curveThisTrl) in the SetAnimationFrames() function.
The key part is in Update(). The initial tone.volume is correct, so I know it's reading that property from the GameObject. I can also see the correct curve values coming through in the Debug Log every frame, so I know the curve evaluation is working. It's just not changing the volume property of the GameObject as I want it to.
Hopefully this is enough information to spot a problem, but if not I can try to provide a test AnimationCurve for reproducibility. Thanks in advance for any help.
public class AnimationControl : MonoBehaviour {
private DataController dataControllerRef;
private CurveDataSingleTrial currentCurveData;
private float initTime;
public AnimationCurve curveThisTrl;
AudioSource tone;
void Awake()
{
initTime = Time.time;
}
// Use this for initialization
void Start ()
{
// Get references to key objects in scene
dataControllerRef = FindObjectOfType<DataController>(); // Has values from JSON
tone = GetComponent<AudioSource>();
Debug.Log(tone.volume);
// query the DataController for the current curve data
currentCurveData = dataControllerRef.GetCurrentCurveData();
SetAnimationFrames();
}
void SetAnimationFrames()
{
int numKeyFrames = currentCurveData.keyframeTimes.Length;
for (int c = 0; c < numKeyFrames; c++)
{
curveThisTrl.AddKey(currentCurveData.keyframeTimes[c], currentCurveData.keyframeVals[c]);
}
}
// Update is called once per frame
void Update ()
{
float currTime = Time.time - initTime;
tone.volume = curveThisTrl.Evaluate(currTime);
Debug.Log(tone.volume);
}
}

Related

Why is my inspector losing reference on play?

This is before play
This is after play.
I have a very simple script. I am using [SerializeField] on a TMP_Text file. In the inspector I have dragged the text file from the Hierarchy into the serlialized field in the inspector of my UI Controller. When I click on play, the inspector drops the reference to the tmp and says there is nothing there. I have the ability to redrag the file once it is running and it will work fine.
Why am I losing the file on play??
[SerializeField] TMP_Text score;
int points;
// Start is called before the first frame update
void Awake()
{
points = 0;
score = GetComponent<TMP_Text>();
}
// Update is called once per frame
void Update()
{
score.SetText(points.ToString());
}
public void AddToScore(int score)
{
points += score;
}
This is a very simple script. The file I am atttaching has no script or anythin to it. It is just a basic text mesh pro object.
In awake you reassign score. The problem is you do a GetComponent which only searches the current GameObject. Since UIController does not contain a TMP_Text component it return null and so you lose the reference. If you just remove that line (score = GetComponent<TMP_Text>();) it will fix your issue

Get Renderer of another game object

I have a 3d cube with a unlit\transparent texture. I'm trying to get access to the material offset parameter.
public class scroll : MonoBehaviour {
public float speed = 0.5f;
public GameObject stars, bg;
public Component ren;
// Use this for initialization
void Start () {
ren = GameObject.Find("stars").GetComponent<Renderer>();
}
// Update is called once per frame
void Update () {
Vector2 offset = new Vector2(0, Time.time * speed);
//here I want to change offset of the texture (shader: unlit\transparent)
}
}
I tried
ren.renderer.material.mainTextureOffeset = offset;
got an Error:
UnityEngine.Material does not contain a definition for
mainTextureOffeset and no extension method mainTextureOffeset of
type UnityEngine.Material could be found. Are you missing an
assembly reference?
structure:
-WORLD (this script attached here)
--stars (3D cube)
You have got a typo in your code: mainTextureOffeset instead of mainTextureOffset. I don't see any other possibilities for UnityEngine.Material not to contain mainTextureOffset.
I think you should use ren.material.mainTextureOffset, since ren is a reference to the renderer of "stars" and not a reference to the GameObject "stars".
EDIT: As #Bagdan Gilevich stated in his answer you should also change mainTextureOffeset to mainTextureOffset.

How to play audioclips in unity from script?

So I am trying to get the sing method to play one of the audioclips I created up top. I know I need an audioSource, I just have no clue how it fits in with the audioClips. I don't currently have an audiosoure assigned to the object that this script is assigned to, so that might impact things. Thanks for your help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Piano : MonoBehaviour {
private List<AudioClip> notes = new List<AudioClip>();
public string voice;
public AudioClip ash1, a1, b1, csh1, c1, dsh1, d1, e1, f1, fsh1, g1, gsh1;
// Use this for initialization
void Start () {
//octave1
notes.Add(a1); notes.Add(b1); notes.Add(ash1); notes.Add(c1); notes.Add(csh1);notes.Add(d1); notes.Add(dsh1);
notes.Add(e1); notes.Add(f1); notes.Add(g1); notes.Add(gsh1);
WarmUp(voice);
}
//consider adding countertenor, true alto (i am using mezzo soprano), and baritone.
void WarmUp(string vp)
{
//high and low end of voice parts
// 30 = c4 for example
int high;
int low;
ArrayList range = new ArrayList();
//c4 to c6
if (vp == "Soprano")
{
high = 0; //this is just a range to make the code shorter
low = 7;
for(int i = low; i < high; i++)
{
range.Add(notes[i]);
}
Sing(range);
}
}
/**
* #Param: range. the arraylist of range of notes.
* shift the pitch UP one half step if the up arrow key is pressed
* shift the pitch down one half step if the down arrow key is pressed
* shift the pitch up a third (4 half steps) if the left arrow key is pressed
* shift the pitch up a fifth (7 half steps) if the right arrow key is pressed
*/
void Sing(ArrayList range)
{
//how to play one of the audioclips in range here
}
// Update is called once per frame
void Update () {
}
}
If you attach an AudioSource component to the GameObject, then you could set an AudioSource variable to that component and then call the setter function clip(AudioClip x) to set the clip the source should play. At that point, you could just call Play() and wait for the length of the audio clip.
Code shouldn't be too difficult to figure out but if you have any more questions, don't hesitate to ask. Good luck!
Attach a AudioSource component to your GameObject.
In Sing:
yourAudioSource.clip = (AudioClip)range[Random.Range(0, range.Count)];
yourAudioSource.Play()
You might also consider changing ArrayList range to List<AudioClip> range or similar to avoid the cast
Take a variable of AudioSource _aSource and AudioClip _aClip and assign the audio source game object to the _aSource and clip to the _aClip on the inspector and where want to play the sound just write _aSource.PlayOneShot(a_Clip) that it.

Spawn sprites on action

I'm trying to make a Pinata GameObject, that when clicked bursts and gives a variable number of Gift GameObjects with various images and behaviors in them.
I'm also not sure what the unity vocabulary for this is so as to look this up in unity docs.
Can anyone please lend me a hand here? Thanks!
There are several ways to handle this.
The simple way is to use Object.Instantiate, Object Instantiation is the vocab you're after.
This will create a copy of a predefined Unity object, this can be a gameobject or any other object derived from UnityEngine.Object, check the docs for more info https://docs.unity3d.com/ScriptReference/Object.Instantiate.html.
In your case, your Pinata would have an array, or list, of prefabs. These prefabs are created by you with a certain behaviour and sprite for each one. When the Pinata bursts, you instantiate random prefabs at random positions surrounding the Pinata, up to you how to position these objects.
Something along these lines should do the trick:
class Pinata : Monobehaviour
{
public GameObject[] pickupPrefabs;
public int numberOfItemsToSpawn; //This can be random
//any other variables that influence spawning
//Other methods
public void Burst()
{
for(int i = 0; i < numberOfItemsToSpawn; i++)
{
//Length - 1 because the range is inclusive, may return
//the length of the array otherwise, and throw exceptions
int randomItem = Random.Range(0, pickupPrefabs.Length - 1);
GameObject pickup = (GameObject)Instantiate(pickupPrefabs[randomItem]);
pickup.transform.position = transform.position;
//the position can be randomised, you can also do other cool effects like apply an explosive force or something
}
}
}
Bare in mind, if you want the game to be consistent, then each behaviour prefab would have there own predefined sprite, this would not be randomised. The only thing randomised would be the spawning and positioning.
If you did want to randomise the sprites for the behaviours then you'd have to add this to the Pinata class:
public class Pinata : Monobehaviour
{
//An array of all possible sprites
public Sprite[] objectSprites;
public void Burst()
{
//the stuff I mentioned earlier
int randomSprite = Random.Range(0, objectSprites.Length - 1);
SpriteRenderer renderer = pickup.GetComponent<SpriteRenderer>();
//Set the sprite of the renderer to a random one
renderer.sprite = objectSprites[randomSprite];
float flip = Random.value;
//not essential, but can make it more random
if(flip > 0.5)
{
renderer.flipX = true;
}
}
}
You can use Unity random for all your random needs, https://docs.unity3d.com/ScriptReference/Random.html
Hopefully this'll lead you in the right direction.

how to randomly initialize particles system in different places of scene in unity3d

I recently tried asking this question but I realized it was not a sufficient question. In my game the player is a fire fighter learner and i want to broke out fire randomly in my game (like not predictable by player), but i did not know how to implement this.
So far i have done this but nothing goes good.(I have a empty object called t in unity which have 3 to 5 particles systems, and all are set to dont awake at start)
code is here :
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public ParticleSystem[] particles;
public int numOn = 3;
public int j;
void Start() {
for (int i = 0; i < particles.Length - 1; i++) {
j = Random.Range(i + 1, particles.Length - 1);
ParticleSystem t = particles[j];
particles[j] = particles[i];
particles[i] = t;
}
for (j = 0; j < numOn; j++ )
{
particles[j].Play();
}
}
}
help will be appreciated :-)
You could try using prefabs. Create a game object in the editor that has any particle systems and scripts your fire objects need. Once it's good, drag the object from the hierarchy into your project. This will create a prefab (you can now remove it from the scene). Now, on your spawning script, add a field of type GameObject and drag the prefab you made before into it. Now, when you need to create one, just call Instantiate(prefabVar) to create a copy of your prefab.
Edit:
For your specific case, since you only want one fire to be instantiated in a random location, you could have your spawning script look something like this:
public Transform[] SpawnPoints;
public GameObject FirePrefab;
void Start() {
Transform selectedSpawnPoint = SpawnPoints[(int)Random.Range(0, SpawnPoints.Count - 1)];
Instantiate(FirePrefab, selectedSpawnPoint.position, selectedSpawnPoint.rotation);
}
This solution would allow for you to potentially spawn more than one fire object if you needed. An alternative would be if you will only ever have exactly one fire object in the scene at all. Instead of instantiating from a prefab, the object is already in the scene and you just move it to one of your spawn points at the start of the scene. An example script on the fire object itself:
public Transform[] SpawnPoints;
void Start() {
Transform selectedSpawnPoint = SpawnPoints[(int)Random.Range(0, SpawnPoints.Count - 1)];
transform.position = selectedSpawnPoint.position;
transform.rotation = selectedSpawnPoint.rotation;
}