- Home /
How to change animation in the code?
I have a dancing charachter created in 3d MAx and imported in Unity. There is a plane attached to his head to skin him with the hat. Some of the hats are a bit bigger - so I want to change the scale of this plane for some of them. I can do it in the visual editor runtime but when I do it with the code - nothing happens! I even wrote something like this
void LateUpdate()
{
m_kHat.renderer.transform.localPosition.Set(10.04f,1f,0.0f);
Debug.Log("m_kHat.renderer.transform.localPosition.x " + m_kHat.renderer.transform.localPosition.x);
}
What I see in the log is m_kHat.renderer.transform.localPosition.x 0.01794739
So it obviously does not change the pos. What do I do wrong?
Answer by stingman · Jul 23, 2012 at 07:45 PM
First off your code is accessing the position of the plane not the scale. You would need to use localScale if you wanted to scale the plane. The best way I can describe doing this is to write down the scale of each plane with each different hat. Then write code to display the appropriate scale depending on which hat is being displayed. Also, if the character can change hats at runtime then using late update is fine as long as you close the call and reset at a later point. Otherwise do this in the Start function to save on memory.
I would also declare variables for each hat with their scale dimensions:
for example:
public GameObject hat; // a plane with the hat texture applied
private float hatScalex = "whatever x value it has"; // adjusts the x value of the scale
void Start() {
if (hat != null)
hat.transform.localScale.x = hatScalex;
//another alternative would be to just declare the total scale in the Start function like so:
hat.transform.localScale += Vector3("your x, y and z values would go here");
}
This is just something to get you started. If you actually did want to edit the position and not the scale then just change localScale to localPosition. Hope this helps clarify a bit
Thanks for the answer! It can be the solution. Finally I found the reason of this weird behaviour. The main problem there was that when I write
m_kHat.renderer.transform.localScale.Set(1.18f,1.1f,1.0f);
it does not change the scale, probably because localScale returns a copy of the actual scale object. If I write the following - it works
m_kHat.renderer.transform.localScale = new Vector3(1.18f,1.1f,1.0f);
Your answer
Follow this Question
Related Questions
set child object position when playing the animation 0 Answers
Syncing object's coordinate position with object's animation position. 3 Answers
unity game 1 Answer
What is causing my attack animation to be in a different position from my idle/run animation? 0 Answers
changing a specific keyframe value 0 Answers