- Home /
How could I make the texture on one object follow the rotation of another object?
In my project, I have a character that has oval-shaped eyes. Because of this, I can't just texture the eyes and rotate them around a center point because they will pop out of the eye sockets. I have tried a few different ways of solving this problem, but it seems that the only way Unity will allow is to texture the eye and attached an invisible object (empty gameobject) to the bone at the center of the eye. Then tell the texture on the eye to move around the eye using the rotation from the empty gameobject.
So far I have deduced that I need to use:
renderer.material.mainTextureOffset = Vector2 (offset, 0);
However, it won't let me use the transform of the empty gameobject as the offset. I get an error saying "Cannot convert 'void' to 'float'. I think I just misunderstand how I should be using this function. Here is the full script I have:
var target : Transform;
var offsetX : float;
var offsetY : float;
function Update ()
{
offsetX = transform.LookAt(target);
offsetY = transform.LookAt(target);
renderer.material.mainTextureOffset = Vector2 (offsetX,offsetY);
}
Or make the eyeballs separate objects that can rotate to point at whatever you want. Rather than trying to offset the texture on them.
$$anonymous$$aybe I didn't explain my problem well enough. The eyes are separate objects from the head so they can actually rotate if I wish them to. But the problem is that they are ovular (egg-shaped), so if I rotate them, the tops poke out of the side of the character's head. Which is why I need to figure out how to make it look like the pupils of the eyes are moving along the surface of the eye without actually rotating the eyes themselves. The only way I think this is possible in Unity is to control the texture on the eyes.
If you look at the doc reference for LookAt(), you'll see the return type is void, so you can't assign that to offsetX.
Possibly you could project the texture from a projector which you could rotate or rewrite a shader, enabling to rotate the uvs.
The projector idea seems pretty solid. I've been playing around with some tutorials that show how to use decals with a projector however there are some weird properties that stop the projection from moving smoothly along the surface of the eye. The decal only wants to rotate in place, which causes it to look like it is slicing into the eye. I really didn't think this would be that difficult. $$anonymous$$aybe the shader idea could work, but unfortunately I don't know the first thing about how to rewrite a shader.
These are the tutorials I am looking at. If anyone knows of a better one that is more suitable for what I am trying to achieve, I would very much appreciate being pointed in the right direction.