Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by richardzzzarnold · Oct 08, 2010 at 03:48 PM · animationguitriggerfollowobjectlabel

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skovacs1 · Oct 08, 2010 at 03:51 PM 0
Share

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

avatar image skovacs1 · Oct 08, 2010 at 03:52 PM 0
Share

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.

avatar image richardzzzarnold · Oct 08, 2010 at 03:53 PM 0
Share

ok sorry about that

avatar image richardzzzarnold · Oct 08, 2010 at 03:53 PM 0
Share

the third try is ok i think

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by skovacs1 · Oct 08, 2010 at 04:52 PM

  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)]); }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image richardzzzarnold · Oct 11, 2010 at 11:25 AM 0
Share

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.

avatar image skovacs1 · Oct 12, 2010 at 03:07 PM 0
Share

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.

avatar image skovacs1 · Oct 12, 2010 at 03:12 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Trigger animation from a GUI button? 1 Answer

triggering random animations with gui 1 Answer

Path Trigger Animation 1 Answer

How do i make a text box appear on screen after a chest has been opened? 3 Answers

ObjectLabel working inversely in y direction 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges