x


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.

more ▼

asked Oct 08 '10 at 03:48 PM

richardzzzarnold gravatar image

richardzzzarnold
72 16 20 28

You only need to format the code as code. Non-code should be left alone.

Oct 08 '10 at 03:51 PM skovacs1

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.

Oct 08 '10 at 03:52 PM skovacs1

ok sorry about that

Oct 08 '10 at 03:53 PM richardzzzarnold

the third try is ok i think

Oct 08 '10 at 03:53 PM richardzzzarnold
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
  1. You cannot define your OnGUI function inside of another function.
  2. You're missing a brace.
  3. GUI doesn't have a property called Texture.
  4. You cannot have a semi-colon inside the array declaration.
  5. 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)]);
}
more ▼

answered Oct 08 '10 at 04:52 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

Thanks for your articulate and carefully explained answer skovacs. As you can see, I know nothing of scripting yet, coming 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.

Oct 11 '10 at 11:25 AM richardzzzarnold

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.

Oct 12 '10 at 03:07 PM skovacs1

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.

Oct 12 '10 at 03:12 PM skovacs1
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3776
x3681
x982
x316
x6

asked: Oct 08 '10 at 03:48 PM

Seen: 1123 times

Last Updated: Oct 08 '10 at 05:25 PM