Unity3D virtual reality development-tag follow function

Source: Internet
Author: User

Project package download: Unity3D virtual reality development-tag follow function

// This is the finished image, which is rough. But the focus is on the implementation of functions.

There is not much to say about the scenario arrangement. directly use the script ~

//move.csusing UnityEngine;using System.Collections;public class move : MonoBehaviour {public float speed=8;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {if (Input.GetKey ("w")) {transform.Translate(Vector3.forward*Time.deltaTime *speed);}if (Input.GetKey ("s")) {transform.Translate(Vector3.back*Time.deltaTime *speed);}if (Input.GetKey ("a")) {transform.Translate(Vector3.left*Time.deltaTime *speed);}if (Input.GetKey ("d")) {transform.Translate(Vector3.right*Time.deltaTime *speed);}    }}
1. the keyboard control code controls the cube in the middle through wsad, that is, the movement of the main role.

2. Declare public speed so that data can be adjusted in real time in the inspector window of unity 3D. This method is useful in actual debugging.

// Tag. csusing UnityEngine; using System. collections; public class tag: MonoBehaviour {// main Camera object public Camera worldcamera; // world Camera // tag Name public string name = "Character's Name "; // tag height float tagHeight; void Start () {// obtain the Camera object worldcamera = Camera. main; // obtain the original height of the model float size_y = transform. getComponent <MeshFilter> (). mesh. bounds. size. y; // get the scaling ratio float scal_y = transform. localScale. y; // their product is the height tagHeight = (size_y * scal_y);} void Update () {} void OnGUI () {// obtain the coordinate Vector3 worldPosition = new Vector3 (transform. position. x, transform. position. y + tagHeight, transform. position. z); // convert the 3D coordinates on the top of the tag to the coordinates Vector2 position = worldcamera In the 2D screen. worldToScreenPoint (worldPosition); // obtain the 2D Coordinate position of the real tag = new Vector2 (position. x, Screen. height-position. y); // calculate the width and height of the tag Vector2 nameSize = GUI. skin. label. calcSize (new GUIContent (Name); // sets the display color GUI. color = Color. black; // draw the tag name GUI. label (new Rect (position. x-(nameSize. x/2), position. y-nameSize. y, nameSize. x, nameSize. y), Name );}}
This script focuses on obtaining the height of the model. There are three methods to obtain it:

(1): Add Collider for the object, and then use XXX. collider. bounds. the size obtained by this method is related to the scaling ratio, which is one-to-one. The size also changes once the scaling ratio changes. So we need to obtain the scale of the object:

float scal_y = transform.localScale.y;

Then multiply by the original height to the actual height.

(2): obtained through the MeshFilter (grid filter), The size attribute of the component can obtain the length of the corresponding x, y, and z directions. This size method has nothing to do with the scaling ratio, the scaling ratio does not change the size. The size records the original size of the object.

The scaling ratio of an object in all directions can be obtained through the localScale of Its transform, that is, XX. transform. localScale. x.

Actual size of the object = original size * scaling ratio

Float xSize = XX. GetComponent (). mesh. bounds. size. x * XX. transform. localScale. x;

(3) obtain the size of a complex object (for example, the root node does not have the MeshFilter, The MeshRenderer component, and the object is composed of many complex and irregular small mesh sub-objects)
For example:

Camera caliber Size

When the projection type is Perspective, the fieldOfView attribute indicates the caliber level, range: [1,179]

When the projection type is Orthgraphic, The orthographicSize attribute is the aperture size in Orthogonal mode.

Finally, assign the two scripts to the main object.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.