- Home /
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 ());
}
}
} }
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.
Attach your GUITexture at object by "door" and show it use function WorldToScreenPoint().
@zharik86 how can i use WorldToScreenPoint() can you give an example?
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.
Your answer
Follow this Question
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