Hi! Can someone help me with this rotation?
I want to align it with the raycast hit.normal rotation…
This is my code:
using UnityEngine;
using System.Collections;
namespace TMPro.Examples
{
public class AlignText: MonoBehaviour
{
public float AngleMultiplier = 1.0f;
public float SpeedMultiplier = 1.0f;
public float CurveScale = 1.0f;
private TMP_Text m_TextComponent;
private bool hasTextChanged;
private struct VertexAnim
{
public float angleRange;
public float angle;
public float speed;
}
void Awake()
{
m_TextComponent = GetComponent<TMP_Text>();
}
void OnEnable()
{
TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
}
void OnDisable()
{
TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
}
void Start()
{
StartCoroutine(AnimateVertexColors());
}
void ON_TEXT_CHANGED(Object obj)
{
if (obj == m_TextComponent)
hasTextChanged = true;
}
IEnumerator AnimateVertexColors()
{
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
Matrix4x4 matrix;
int loopCount = 0;
hasTextChanged = true;
VertexAnim[] vertexAnim = new VertexAnim[1024];
for (int i = 0; i < 1024; i++)
{
vertexAnim[i].angleRange = Random.Range(10f, 25f);
vertexAnim[i].speed = Random.Range(1f, 3f);
}
TMP_MeshInfo[] cachedMeshInfo = textInfo.CopyMeshInfoVertexData();
int characterCount = textInfo.characterCount;
if (characterCount == 0)
{
yield return new WaitForSeconds(0.25f);
}
for (int i = 0; i < characterCount; i++)
{
TMP_CharacterInfo charInfo = textInfo.characterInfo[i];
if (!charInfo.isVisible)
continue;
VertexAnim vertAnim = vertexAnim[i];
int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
int vertexIndex = textInfo.characterInfo[i].vertexIndex;
Vector3[] sourceVertices = cachedMeshInfo[materialIndex].vertices;
Vector2 charMidBasline = (sourceVertices[vertexIndex + 0] + sourceVertices[vertexIndex + 2]) / 2;
Vector3 offset = charMidBasline;
Vector3[] destinationVertices = textInfo.meshInfo[materialIndex].vertices;
RaycastHit hit;
if (Physics.Raycast(offset, Vector3.forward, out hit))
Debug.DrawLine(offset, hit.point, Color.green, 10.0f, false);
float offs = -7.5f;
destinationVertices[vertexIndex + 0].z = hit.point.z + offs;
destinationVertices[vertexIndex + 1].z = hit.point.z + offs;
destinationVertices[vertexIndex + 2].z = hit.point.z + offs;
destinationVertices[vertexIndex + 3].z = hit.point.z + offs;
}
for (int i = 0; i < textInfo.meshInfo.Length; i++)
{
textInfo.meshInfo[i].mesh.vertices = textInfo.meshInfo[i].vertices;
m_TextComponent.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
}
loopCount += 1;
yield return new WaitForSeconds(0.1f);
}
}
}