Noob here… I am working with a c# script in Unity which was originally written to scale an object on 2 separate axes (X/Z) using 2 separate variables (based on audio input). The change I am trying to make is to have that object scale in all axes uniformly.
I tried changing the variables from Floats to Vec3 and removed the trailing x and z in the localScales on the last line to make it uniform scaling. But as the title mentions, Unity says it can’t convert a Vec3 to a float. I think I understand the problem, but not the solution. I’ve looked at other similar posts and experimented but have come up empty.
I foresee changing these 2 variables to be uniform XYZ scales will multiply the effect — I think I’m okay with that, although I may want to try to make it use a single variable to do the scaling eventually.
Thank you to anyone willing to help me, I’ve been learning Python but haven’t gotten much into c# yet.
This is what the code looks like now (includes my changes):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static SAP.SapVars;
public class GrowSap : MonoBehaviour
{
private RedactedForPrivacy sap;
public SapSetBand growBand;
public int band;
public Vector3 height;
public Vector3 baseHeight;
private float _spectrumBand;
// Start is called before the first frame update
void Start()
{
sap = RedactedForPrivacy._SapInstance;
}
// Update is called once per frame
void Update()
{
switch(growBand.bandType)
{
case BandType.Raw:
_spectrumBand = sap.GetSpectrumBand(growBand.source, growBand.spectrum, band);
break;
case BandType.Smooth:
_spectrumBand = sap.GetSpectrumBandSmooth(growBand.source, growBand.spectrum, band);
break;
case BandType.Trigger:
_spectrumBand = sap.GetSpectrumBandTrigger(growBand.source, growBand.spectrum, growBand.trigger, band);
break;
}
this.transform.localScale = new Vector3(this.transform.localScale, baseHeight + _spectrumBand * height, this.transform.localScale);
}
}
This is what the original code looked like (height and baseHeight were formerly floats, and the local scale transformations on the last line stipulated .x and .z axes)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static SAP.SapVars;
public class GrowSap : MonoBehaviour
{
private RedactedForPrivacy sap;
public SapSetBand growBand;
public int band;
public float height;
public float baseHeight;
private float _spectrumBand;
// Start is called before the first frame update
void Start()
{
sap = RedactedForPrivacy._SapInstance;
}
// Update is called once per frame
void Update()
{
switch(growBand.bandType)
{
case BandType.Raw:
_spectrumBand = sap.GetSpectrumBand(growBand.source, growBand.spectrum, band);
break;
case BandType.Smooth:
_spectrumBand = sap.GetSpectrumBandSmooth(growBand.source, growBand.spectrum, band);
break;
case BandType.Trigger:
_spectrumBand = sap.GetSpectrumBandTrigger(growBand.source, growBand.spectrum, growBand.trigger, band);
break;
}
this.transform.localScale = new Vector3(this.transform.localScale.x, baseHeight + _spectrumBand * height, this.transform.localScale.z);
}
}
Thank you to anyone willing to help me
Jeremy Romanowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
The main issue is that Vector3 and float are completely different types, a float is a single decimal value, while Vec3 is a set of 3 ordered vectors/decimals, representing 3 axis: x, y, and z (e.g. <0.1, 0.1, 0.1>).
In the following line:
this.transform.localScale = new Vector3(this.transform.localScale, baseHeight + _spectrumBand * height, this.transform.localScale);
There are 2 main issues:
First, you’re trying to multiply height and baseHeight which are sets of 3 values, by a float: _spectrumBand to be assigned as an axis (a float) of a new Vector3.
Secondly, by removing the axis specifiers (.x, and .z) from this.transform.localScale, means that you’re now using the whole vector as the value for the single axis.
So the solution is to convert this vector to a float by some equation, like through the magnitude of the vector for example.
Vector3 newVec = baseHeight + _spectrumBand * height;
this.transform.localScale = new Vector3(this.transform.localScale.x, newVec.magnitude, this.transform.localScale.z);
Or any function of the different axis of these vectors as single values for each axis in the resulting this.transform.localScale
vector.
Hope this helps!