- Home /
Adjusting ObjectLabel to activate animations
I am using the ObjectLabel script from wiki unify to keep a GUI texture floating over a characters in my scene. But I cannot work out how to use that GUI textures to behave as a trigger so as to trigger animation in the character it is floating over. I have adapted the ObjectLAbel script like this
var piggy_a : newArray("jump";"hop","dance");
var target : Transform; // Object that this label should follow
var offset = Vector3.up; // Units in world space to offset; 1 unit above object by default var clampToScreen = false; // If true, label will be visible even if object is off screen var clampBorderSize = .05; // How much viewport space to leave at the borders when a label is being clamped var useMainCamera = true; // Use the camera tagged MainCamera var cameraToUse : Camera; // Only use this if useMainCamera is false private var cam : Camera; private var thisTransform : Transform; private var camTransform : Transform;
function Start () { thisTransform = transform; if (useMainCamera) cam = Camera.main; else cam = cameraToUse; camTransform = cam.transform; }
function Update () { if (clampToScreen) { var relativePosition = camTransform.InverseTransformPoint(target.position); relativePosition.z = Mathf.Max(relativePosition.z, 1.0); thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset)); thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize), Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize), thisTransform.position.z); } else { thisTransform.position = cam.WorldToViewportPoint(target.position + offset);
function OnGUI() {
if(GUI.Texture){ animation.CrossFade(piggy_a[Random.Range(0,piggy_a.length)]); } }
}
just putting a new array at the top and at the bottom trying to blend the script i would use for a conventional button. But i do not know how to make the floating texture the active GUI button. I dont know enough about scripting.
You only need to format the code as code. Non-code should be left alone.
Also, please don't create and then delete and then re-post the same question when all you did was change formatting that had already been changed. That's what the edit button is for.
Answer by skovacs1 · Oct 08, 2010 at 04:52 PM
- You cannot define your OnGUI function inside of another function.
- You're missing a brace.
- GUI doesn't have a property called Texture.
- You cannot have a semi-colon inside the array declaration.
- Are you animating this transform? Does this transform have those animations on it even?
You did more than just add an array. The changes you made to the original code are a mess and won't work. If you make changes, test them as you go and fix any errors.
Using a GUITexture
If you really want to use a GUITexture use one. If you want to add interaction, then you would implement OnMouse functions like OnMouseUp(). You only needed 3 changes to the original to make it do what you wanted:
//Add this var anims : String[] = ["jump","hop","dance"]; //animations
//Add this function OnMouseUp() { target.animation.CrossFade(anims[Random.Range(0,anims.length)]); }
//Change this at the end @script RequireComponent(GUITexture)
Using a GUI.Button
The way the original script worked was that it would have an attached GUIText object. To use a button, you don't need to worry about the transform.
This will do generally what you want if you attach it to something you want the button to follow:
var anims : String[] = ["jump","hop","dance"]; //animations var buttonTexture : Texture; //The texture to use on the button var offset = Vector3.up; //World space offset. 1 unit above object by default var clampToScreen = false; //Will the label be visible if object is off screen var clampBorderSize = .05; //screen space to leave when clamped var useMainCamera = true; //Use the camera tagged MainCamera var cameraToUse : Camera; //Camera to use if useMainCamera is false private var cam : Camera; //The camera we're using private var screenPos : Vector3; //The screen position of the button
function Start () { if(useMainCamera) cam = Camera.main; else cam = cameraToUse; }
function Update () { if(clampToScreen) { var relativePosition = cam.transform.InverseTransformPoint(transform.position); relativePosition.z = Mathf.Max(relativePosition.z, 1.0); screenPos= cam.WorldToScreenPoint(cam.transform.TransformPoint(relativePosition + offset)); screenPos= Vector3(Mathf.Clamp(screenPos.x, clampBorderSize,Screen.width-(clampBorderSize+buttonTexture.width)),Mathf.Clamp(screenPos.y,clampBorderSize,Screen.height-(clampBorderSize+buttonTexture.height)),screenPos.z); } else screenPos= cam.WorldToScreenPoint(transform.position + offset); }
function OnGUI() { if(GUI.Button(Rect(screenPos.x,screenPos.y,buttonTexture.width,buttonTexture.height),buttonTexture)) animation.CrossFade(anims[Random.Range(0,anims.length)]); }
Thanks for your articulate and carefully explained answer skovacs. As you can see, I know nothing of scripting yet, co$$anonymous$$g from maya background. I am wondering if it possible to place the GUI.Button above a child node of the animation object ? I looked at the GetComponentInChildren function but can't work out how to apply it.
In the GUITexture version of the script above, you could just drop the child into the target, or in the GUI.Button version, you could attach the script to the child directly.
You wouldn't necessarily need GetComponent... code as that is for components like scripts, colliders, rigidbodies, etc., unless you are looking for a child with a given component and maybe to get some position from the component. If you need to simply find a child, the children are contained in the parent transform and can be accessed with for(var child:Transform in target){//do something with child} as per the docs on Transform. If you need to find a child with a component, you would do var component:Type=gameObject.GetComponentInChildren(Type); //do something with component.transform.