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
1
Question by ChazAshley1 · Jun 11, 2014 at 02:37 PM · guilabelworldtoscreenpoint

Wrong WorldToScreenPoint converting (not inverted y)

Hey. I have GameObject under character head and want to see character's nickname in this position. Here my code

 using System;
 using SCdll.NSServer;
 using SCdll.NSServer.ServerInterface;
 using UnityEngine;
 using System.Collections;
 
 public class ClientControl : MonoBehaviour
 {
 
     public CharInfo newData, lastData, info;
     private Animator animator;
     public float speed = 1;
     private Transform myTransform;
     private CharacterController controller;
     private Vector3 velocity, pointPos;
     public float time;
     public ushort messageID = 0;
     public int UserID;
     private GUIStyle style;
     private Transform nickPoint;
     private Vector3 screen;
     private Vector2 nickSize;
     private float distance;
     private float multiplier;
     private float x;
     private float y;
     private Vector3 viewPortPos;
     private Transform mesh;
 
     // Use this for initialization
     private void Start()
     {
         mesh = transform.FindChild("mesh");
         animator = transform.FindChild("mesh").GetComponent<Animator>();
         myTransform = transform;
         velocity = Vector3.zero;
         controller = transform.GetComponent<CharacterController>();
 
         style = new GUIStyle();
         style.alignment = TextAnchor.LowerCenter;
         style.normal.textColor = Color.black;
         style.fontSize = 19;
         style.fontStyle = FontStyle.Bold;
         nickPoint = transform.FindChild("nick");
         screen = Camera.main.ViewportToScreenPoint(Camera.main.WorldToViewportPoint(nickPoint.transform.position));
         nickSize = style.CalcSize(new GUIContent(TempStorage.nickname));
         x = -11/90f;
         y = 182/9f;
     }
 
     // Update is called once per frame
     void Update()
     {
         distance = Vector3.Distance(myTransform.position, Camera.main.transform.position);
         if (distance <= 10)
         {
             style.fontSize = 19;
         }
         else if (distance >= 100)
         {
             style.fontSize = 8;
         }
         else
         {
             style.fontSize = (int) Math.Floor(distance*x + y);
         }
        // Debug.Log("Font size is: " + style.fontSize + " Distance is: " + distance);
     }
 
     void OnGUI()
     {
         if (info != null)
         {
             viewPortPos = Camera.main.WorldToViewportPoint(nickPoint.transform.position);
            // Debug.Log("viewPortPos: " + viewPortPos);
             if (viewPortPos.x >= 0 && viewPortPos.x <= 1 && viewPortPos.y >= 0 && viewPortPos.y <= 1 && viewPortPos.z>0
                 && !Physics.Linecast(nickPoint.transform.position,Camera.main.transform.position) && distance<=100)
             {
                 screen = Camera.main.WorldToScreenPoint(nickPoint.transform.position);
                 GUI.Label(new Rect(screen.x - 25, Screen.height-screen.y, 50, 25), info.NickName,style);
             }
         }
 
     }
 }

What is my problem? When the character goes far away then nickname goes down. I don't know why, really, no idea... Maybe someone knows why it happens? p.s. here is my video proof, pink sphere - point where nickname must be VIDEO

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 Andres-Fernandez · Jun 11, 2014 at 02:39 PM 0
Share

Is the label top anchored?

avatar image robertbu · Jun 11, 2014 at 03:48 PM 1
Share

@Andres Fernandez has spotted the likely right issue. Your text is top anchored. You are going to need to get the text height and move the anchor to the bottom of the text.

avatar image ChazAshley1 · Jun 11, 2014 at 03:55 PM 0
Share

PLEASE WATCH THE CODE

  style.alignment = TextAnchor.LowerCenter;

0 Replies

· Add your reply
  • Sort: 

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dynamic Resizing of tooltip to match content? 1 Answer

(GUI.Label) Line spacing issue with wordwrap 0 Answers

Making a text box appear after a certain gameObject is destroyed 0 Answers

Relate font size to screen size in GUISkin 2 Answers

Show the int/float value in GUI.Label 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