Putting Glasses on animation

I have this guy moving, but now i want to put glasses on him, how do i put the “glasses” model so it can “follow” the head movement?

Usually this is not done with scripting, per se, but in the model. Model your character AND the glasses. Make the glasses a separate mesh and have them turned off when the character is instantiated. When you need glasses, turn on the mesh for the glasses, and as they are already part of the character and animated with it, they will follow te character.

Make sense?

If they are already made and a separate model, then parent them to the charcter’s head and use the same technique.

Nice!

But what if i have lots of glasses?

I already tried to Instantiate but they wont get the animation flow, they start moving with the character but its aways instantiated in the same place and do not match with the animation sometimes

I would do it all in animation. but with a twist. Have a Two pair of glasses in your animation. one on the head, visible, on in the hand, invisible. When you want the character to take them off, play an animation that says “Take off Glasses1” It reaches up to the glasses. When that one ends, you turn GlassesOn=false, and start “Take off Glasses2”.

Same for the reverse, “Put on Glasses1” and “Put on Glasses 2”

Now, in Unity, both of those glasses are “turned off” and a pair of glasses are substituted. Your character would have a simple variable, GlassesOn=true; So in your LateUpdate…

var GlassesOn=true;
var CurrentGlasses:Transform;
var HeadGlassPoint:Transform;
var HandGlassPoint:Transform;

function LateUpdate(){
if(GlassesOn){
CurrentGlasses.position=HeadGlassPoint.position;
CurrentGlasses.rotation=HeadGlassPoint.rotation;
}else{
CurrentGlasses.position=HandGlassPoint.position;
CurrentGlasses.rotation=HandGlassPoint.rotation;
}
}

So you would have one pair of glasses associated with the player, they can be on, or off, it will always follow the hand or head. Later on, you can simply swap the CurrentGlasses for any model you wish at that point.

Thanks BigMisterB but my situation is much more simple, i’m doing a simple customization in a toon character and want to add itens from a side menu, the character is aways bouncing a little, the couse that the glasses wont match,

The approach them is to have all sort off accessories already in the model and simple turn ON the renderer?

Ah, then it can be done in either way. Put them in the model, or have them external. both ways would work fine.

As a matter of fact, if the glasses had fluff, it would be better if they were animated with the character.

Do Elton John proud…

I would attach your instantiated glasses to an appropriate bone of the character model. You will need to play with your local position/rotation values to get things looking right, and they will vary depending on which bone you attach to. The code for a component on your character object might look something like this (shortened bone hierarchy for legibility):

handBone = transform.Find("Root/Spine/R Arm/R Hand");
glasses = (GameObject)Instantiate(glassesPrefab, Vector3.zero, Quaternion.identity);
glasses.transform.parent = handBone;
animation.Play("PutGlassesOn");

And once that animation is done playing (e.g. after some time delay) you might then:

headBone = transform.Find("Root/Spine/Neck/Head");
glasses.transform.parent = headBone;

As I said though, in both cases you may have to play with glasses.transform.localPosition and .localRotation before things look good. You can experiment with values for these in the editor by simply temporarily attaching your glasses object directly to one of the bones via Inspector, then playing with its Transform numbers.

Thanks Little Angel and BigMisterB

pixels, my glasses are child of the head bone, they move naturally if they started along the bone, the great problem is to “match” the instantiate process, “play with this transform numbers” looks difficult hehe,

I’ll put every thing i want and them just turn the renderer on/off, i think memory for just some very low poly will not be a problem, even if i working with iOS.

Thanks everyone.