[英]Unity How to change unity's 3D Object parameters via C# script
[英]How can I change the scale of a 3D Object and the Audio Source's radius in a proportionally way in Unity 5 using C#?
我正在使用Array在空間中隨機乘以3D對象,因此我得到了許多漂亮的對象,它們隨機地沿y,x和z軸浮動。 這個對象還有一個帶有聲音的音頻源,這意味着在應用隨機數組定位后,我也會得到帶有不同音頻源的不同對象。
問題是我也在更改這些對象的比例,它的工作效果非常好,但是音頻源的大小/比例/半徑根本沒有改變。
如何更改對象的比例並同時更改音頻源的尺寸/比例/半徑,以使尺寸相等或成比例地匹配?
我在這里看,但我不知道。 https://docs.unity3d.com/ScriptReference/AudioSource.html
這是我正在使用的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class multipleObjectsGrandes : MonoBehaviour {
public GameObject prefabGrandes;
public GameObject[] gos;
public int cantidad;
public float minScaleObj;
public float maxScaleObj;
void Awake()
{
gos = new GameObject[cantidad];
for(int i = 0; i < gos.Length; i++)
{
//Position
Vector3 position = new Vector3(Random.Range(-40.0f, 40.0f), Random.Range(-40.0f, 40.0f), Random.Range(-40.0f, 40.0f));
GameObject clone = (GameObject)Instantiate(prefabGrandes, position, Quaternion.identity);
//Scale
clone.transform.localScale = Vector3.one * Random.Range(minScaleObj, maxScaleObj);
//Rotation
clone.transform.rotation = Quaternion.Euler(Random.Range(0.0f, 360.0f), Random.Range(0.0f, 360.0f), Random.Range(0.0f, 360.0f));
gos[i] = clone;
}
}
}
這是我用來乘法對象的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class multipleObjectsGrandes : MonoBehaviour
{
public GameObject prefabGrandes;
public GameObject[] cuerpos;
public int cantidad;
public float minScaleObj;
public float maxScaleObj;
public float escalaMax;
void Awake ()
{
cuerpos = new GameObject[cantidad];
for (int i = 0; i < cuerpos.Length; i++) {
//Position
Vector3 position = new Vector3 (Random.Range (-40.0f, 40.0f), Random.Range (-40.0f, 40.0f), Random.Range (-40.0f, 40.0f));
GameObject clone = (GameObject)Instantiate (prefabGrandes, position, Quaternion.identity);
//Scale
clone.transform.localScale = Vector3.one * Random.Range (minScaleObj, maxScaleObj);
//Rotation
clone.transform.rotation = Quaternion.Euler (Random.Range (0.0f, 360.0f), Random.Range (0.0f, 360.0f), Random.Range (0.0f, 360.0f));
escalaMax = clone.transform.localScale.x;
//Debug.Log(escalaMax);
}
}
}
當我在控制台中調試“ escalaMax”時,此處一切正常。
變量“ escalaMax”是我要傳遞給另一個腳本的變量,該腳本位於我正在乘法的對象中,該對象具有附加的audioSource。 這是腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(AudioSource))]
public class AudioRandomPitch : MonoBehaviour
{
public float startingPitch = 1;
public float minPitchRandom = 0;
public float maxPitchRandom = 0;
AudioSource audio;
public float audioScale;
void Start ()
{
startingPitch = Random.Range (minPitchRandom, maxPitchRandom);
audio = GetComponent<AudioSource> ();
audio.pitch = startingPitch;
GameObject theMultiplicator = GameObject.Find ("Multiplicador_deCuerpos");
multipleObjectsGrandes multiplicatorScript = theMultiplicator.GetComponent<multipleObjectsGrandes> ();
multiplicatorScript.escalaMax = audioScale;
//this is not working
audio.maxDistance = audioScale;
Debug.Log(audioScale);
}
}
在這里,我首先將來自其他腳本escalaMax的值保存在新變量audioScale中。 現在的問題是當我嘗試將audioScale值傳遞給audio.maxDistance var時。
我知道我非常親密,但是如您所見,我不是一個非常好的程序員,而且我可能做錯了一些愚蠢的事情...:/
謝謝您的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.