Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// Класс для поворота канваса и объектов на камеру, вставляет внешний контейнер между собой и объектом
- /// </summary>
- public class CameraFacingBillboard : MonoBehaviour
- {
- public Camera Camera;
- //added in-between GRP object to perform rotations on
- public GameObject Container;
- private Transform myParrent;
- void Awake(){
- if (GameObject.ReferenceEquals(Camera, null)) Debug.LogError("CameraFacingBillboard : Camera is null");
- myParrent = transform.parent;
- Container = new GameObject();
- Container.name = "GRP_" + transform.gameObject.name;
- Container.transform.position = transform.position;
- Container.transform.SetParent(myParrent);
- Container.transform.localScale = Vector3.one * (1 / myParrent.lossyScale.x);
- transform.SetParent(Container.transform);
- transform.localRotation = Quaternion.identity;
- }
- public float Height;
- private const float ChangeHeightKoeff = 2f;
- void LateUpdate()
- {
- Container.transform.LookAt(Container.transform.position + Camera.transform.localToWorldMatrix.MultiplyVector(Vector3.forward), Camera.transform.localToWorldMatrix.MultiplyVector(Vector3.up));
- Vector3 pos = Container.transform.localPosition;
- if (Mathf.Abs(pos.y - Height) > .01f)
- {
- pos = new Vector3(pos.x, pos.y + (pos.y < Height ? 1 : -1) * (Mathf.Min(Time.deltaTime * ChangeHeightKoeff, Mathf.Abs(pos.y - Height))), pos.z);
- Container.transform.localPosition = pos;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment