romanilyinfb

CameraFacingBillboard.cs

Sep 6th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. /// <summary>
  6. /// Класс для поворота канваса и объектов на камеру, вставляет внешний контейнер между собой и объектом
  7. /// </summary>
  8. public class CameraFacingBillboard : MonoBehaviour
  9. {
  10.     public Camera Camera;
  11.     //added in-between GRP object to perform rotations on
  12.     public GameObject Container;
  13.     private Transform myParrent;
  14.  
  15.     void Awake(){
  16.         if (GameObject.ReferenceEquals(Camera, null)) Debug.LogError("CameraFacingBillboard : Camera is null");
  17.         myParrent = transform.parent;
  18.         Container = new GameObject();
  19.         Container.name = "GRP_" + transform.gameObject.name;
  20.         Container.transform.position = transform.position;
  21.         Container.transform.SetParent(myParrent);
  22.         Container.transform.localScale = Vector3.one * (1 / myParrent.lossyScale.x);
  23.         transform.SetParent(Container.transform);
  24.         transform.localRotation = Quaternion.identity;
  25.     }
  26.  
  27.     public float Height;
  28.     private const float ChangeHeightKoeff = 2f;
  29.  
  30.  
  31.     void LateUpdate()
  32.     {
  33.         Container.transform.LookAt(Container.transform.position + Camera.transform.localToWorldMatrix.MultiplyVector(Vector3.forward), Camera.transform.localToWorldMatrix.MultiplyVector(Vector3.up));
  34.         Vector3 pos = Container.transform.localPosition;
  35.         if (Mathf.Abs(pos.y - Height) > .01f)
  36.         {
  37.             pos = new Vector3(pos.x, pos.y + (pos.y < Height ? 1 : -1) * (Mathf.Min(Time.deltaTime * ChangeHeightKoeff, Mathf.Abs(pos.y - Height))), pos.z);
  38.             Container.transform.localPosition = pos;
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment