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 StatecLata · Nov 09, 2014 at 06:34 PM · guitexturebuttontouchgui-texture

GuiTexture (Touch button) unfollow camera!

I've created a Gui Texture (button) that will allow the object to enter a door (by touching it on the android device and spawn at another door, the thing is i want that gui texture (button) to stay on the door and not follow the CAMERA around is that possible?

Thanks!

using UnityEngine; using System.Collections;

public class Teleport : MonoBehaviour {

 public Transform Characters;
 Animator anim;
 
 public Transform GroundCheck;
 public Transform playerCheck;
 float playerRadius = 0.5f;
 public LayerMask whatIsPlayer;
 public Transform Teleport2;
 bool Teleported1 = false;
 public GUITexture guiEnterDoor;
 private bool EnterDoor = false;
 // Use this for initialization
 void Start () {
 
     anim = GetComponent<Animator>();


 }

 IEnumerator Wait() {
     
     Debug.Log("Before Waiting 1 seconds");
     yield return new WaitForSeconds (1);
     Characters.transform.position = Teleport2.transform.position;
     anim.SetBool ("EnteringDoor", false);
     Debug.Log("After Waiting 1 Seconds");
 }

 void Update() {
             if (Input.touchCount > 0) {
                     // Get the touch info
                     Touch t = Input.GetTouch (0);
         
                     // Did the touch action just begin?
                     if (t.phase == TouchPhase.Began) {

                             // Are we touching the Door?
                             if (guiEnterDoor.HitTest (t.position, Camera.main)) {
                                     Debug.Log ("Touching EnterDoor");
                                     EnterDoor = true;
                                     
                                     }
                 
                             }
         // Did the touch end?
         if (t.phase == TouchPhase.Ended)
         {
             
             // Stop all movement
             EnterDoor = false;
             rigidbody2D.velocity = Vector2.zero;
         }
                     }
             }
     

 // Update is called once per frame
 void FixedUpdate () {
 
     
     Teleported1 = Physics2D.OverlapCircle (GroundCheck.position, playerRadius, whatIsPlayer);
     anim.SetBool ("DOOR1", Teleported1);

 if (EnterDoor)
 {

         if (Teleported1) {
             StartCoroutine(Wait ());
         }

 }


} }

Comment
Add comment · Show 3
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 MrSoad · Nov 09, 2014 at 04:13 PM 1
Share

Can you be more specific about what it is you are doing(what do you mean by "I've created a Gui Texture that will allow the object to enter a door", and post your code as well please, thanks.

avatar image zharik86 · Nov 10, 2014 at 08:00 AM 0
Share

Attach your GUITexture at object by "door" and show it use function WorldToScreenPoint().

avatar image StatecLata · Nov 10, 2014 at 01:10 PM 0
Share

@zharik86 how can i use WorldToScreenPoint() can you give an example?

1 Reply

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

Answer by zharik86 · Nov 10, 2014 at 07:28 PM

Ok, I write simple example(write on CSharp) and this script must be attach at object "door":

  public Texture2D myTexture = null; //reference for your texture for GuiTexture
  private GUITexture myGUITex = null;

  void Start() {
   //Create programmical guiTexture, which parent is our door
   GameObject gmo = new GameObject(); //create new object
   gmo.name = "myTexForDoor";
   gmo.parent = this.transform; //parenting our object
   //Position with GUITexture on world must be (0, 0, 0)
   gmo.transform.position = new Vector3(0, 0, 0);
   gmo.ltransform.localScale = new Vector(0, 0, 1);
   myGUITex = gmo.AddComponent<GUITexture>(); //create GUITexture
   myGUITex.texture = myTexture;
   //For example, our GUITexture have size 50x50
   myGUITex.pixelInset = new Rect(-100, -100, 50, 50);
  }

  void Update() {
   //Simple check for touch - only first touch
   //But remember, touch not work in Editor, only real device or emulator android AVD
   if (Input.touchCount > 0) {
    if (Input.GetTouch(0).phase == TouchPhase.Began) {
     if (myGUIText.HitTest(Input.GetTouch(0).position) {
      //Open or close your door
     }
    }
   }
  }

  void LateUpdate() {
   //Show our texture depending on position of the main camera
   //See pivot point of door
   Vector3 pos = Camera.main.WorldToScreenPoint(this.transform);
   //Set position, because door maybe open/close - change position
   myGUITex.transform.position = new Vector3(0, 0, 0);
   //Set right position on screen
   myGUITex.pixelInset = new Rect(pos.x, pos.y, 50, 50);
  }

It's common example. I hope that it will help you.

Comment
Add comment · Show 1 · 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 StatecLata · Nov 11, 2014 at 02:11 PM 0
Share

I figured it out another way thanks a ton though!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Drag and Drop button on Mobile (Messenger style) 0 Answers

Assiging an Image to a Button 1 Answer

Make a Button out of a Textured Plane 2 Answers

Buttons and images 1 Answer

Holding GUI Button Touch to Rotate Object 0 Answers


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