前言
- 这个效果是在以前的项目时候,特效那边想要一个广告牌效果
- 但是我不懂什么是广告牌
- 两个人沟通半天,才把东西做出来。
代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class LooAt : MonoBehaviour
{
public bool IsActive;
public Transform targetCamera;
void Start ()
{
#if UNITY_EDITOR
EditorApplication.update += LookAtCamera;
#endif
}
private void Update ()
{
if (Input.GetKeyDown (KeyCode.A))
{
Debug.Log (transform.localEulerAngles.x);
}
}
void LookAtCamera ()
{
if (IsActive && targetCamera != null)
{
Vector3 curLocalTarPos = transform.InverseTransformPoint (targetCamera.position);
Vector3 newPos = new Vector3 (curLocalTarPos.x, 0, curLocalTarPos.z);
float angle = Vector3.Angle (Vector3.forward, newPos);
float angleDir = Vector3.Cross (Vector3.forward, newPos).y;
angleDir = angleDir == 0 ? angleDir : angleDir / Mathf.Abs (angleDir);
angle = angle * angleDir;
transform.Rotate (Vector3.up, angle, Space.Self);
}
}
private void OnDestroy ()
{
#if UNITY_EDITOR
EditorApplication.update -= LookAtCamera;
#endif
}
}