Search Unity

Character Name Displayng

Discussion in 'UGUI & TextMesh Pro' started by Zathos91, Nov 16, 2014.

  1. Zathos91

    Zathos91

    Joined:
    Aug 26, 2014
    Posts:
    45
    Hi guys!

    I have another problem with 4.6 UI and character prefab...
    I want to add names under the foots of every character that connects and plays the game. I already was able to put the name correctly under the foots and follows the character movement also...the problem is that follow also the ROTATION! making strange behaviour....i'll give you screenshots so you can understand better the situation

    CORRECT ONE

    NOT CORRECT

    This happens as you can see when i start to rotate the character with mouse...

    Any way how can i solve this?
    The canvas that manages the character name is of course a child of the object!
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You need to show the rotation code.
     
  3. Zathos91

    Zathos91

    Joined:
    Aug 26, 2014
    Posts:
    45
    Code (CSharp):
    1. void Turning()
    2.     {
    3.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    4.         RaycastHit floorHit;
    5.         //CheckDirection ();
    6.         if (Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) {
    7.             playerToMouse = floorHit.point - transform.position;
    8.             playerToMouse.y = 0f;
    9.             Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
    10.             Debug.DrawRay(transform.position,playerToMouse,Color.green);
    11.             playerRigidBody.MoveRotation (newRotation);
    12.         }
    13.         Quaternion newRotation = GetComponentInChildren<Aiming>().getAngle ();
    14.         playerRigidBody.MoveRotation(newRotation);
    15.  
    16.     }
    17.  
    This is what is attached to my PlayerPrefab, it's basically similiar to the Nightmare Game Tutorial from Unity.

    While the script that actually "tries" to fix the child Canvas rotation is :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFacingBillBoard : MonoBehaviour {
    5.  
    6.     Camera m_Camera;
    7.  
    8.     void Awake(){
    9.         m_Camera = Camera.main;
    10.     }
    11.     void LateUpdate()
    12.     {
    13.         transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
    14.                          m_Camera.transform.rotation * Vector3.up);
    15.     }
    16. }
    17.  

    Anyway i dont think the problem is in the code of the Player Rotation...the fact is that the Canvas is a child of the player and "follows" every its change....thing that i want to kept for position but not for rotation!
     
  4. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    Don't make the Canvas a child of the player, just update it's position manually.
     
  5. Zathos91

    Zathos91

    Joined:
    Aug 26, 2014
    Posts:
    45
    Yeah it may be a solution...but my game is a Multiplayer one and it will be a LOT easier if the canvas is a child of the player so when a player is istanceted also the canvas is.

    Is there really no solution to this behaviour?
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Both are practical, but there seems to be a problem with your rotation. I have the impression that you shouldn't rotate the text itself, but instead create a parent object for the text that is then rotated. In order to adjust the position of the text, you may then offset the only e.g. in the z direction relative to its parent object. The parent object itself is supposed to be below the character.
    With this setup you can heavily simplify the rotation code to something like that:
    Code (csharp):
    1. transform.rotation = m_Camera.rotation;
     
  7. Zathos91

    Zathos91

    Joined:
    Aug 26, 2014
    Posts:
    45
    Man your idea of putting the text inside an empty game object worked! But really i cant understand why, do you mind if you have time to explain me better? ....

    thanks a lot anyway!!!!!!!
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I made a AAA drawing to illustrate the idea.

    The idea is that whenever the camera rotation changes, the Empty game object will be rotated too and with it the Text. Theoretically you could achieve the same with just one game object, but this is a straight forward approach. Picasso.jpg
     
    Zathos91 likes this.