I am trying to fix a gameobject for e.g. a cube (which i am using as a sensor) on my character. I need the cube to rotate (with orientation control from the inspector - I have a public quaternion rotation variable set). I have identified 3 vertices using ray cast to fix my sensor onto the character. I already have determined the normal of these vertices which will be the "up" direction of the cube. I am wondering how I can determine the forward/right vector to use in the LookRotation() function. I need to basically be able to rotate the cube ON the character even as the character moves on animation. This is my code, and as of now there is no rotation happening. I'm sure I am missing something and would really appreciate help. I really hope this is enough explanation.
using System.Collections;
using System.Collections.Generic; using UnityEngine;
public class orientation : MonoBehaviour
{
// importing from Vertex Finder Script to use InterpolatedNormalInWorldSpace value
private VertexFinder script;
public Transform target;
// to edit orientation of sensor in Unity Inspector
public Quaternion rotation_edit;
// Start is called before the first frame update
void Start()
{
script = GetComponent<VertexFinder>();
}
Quaternion TurretLookRotation(Vector3 approximateForward, Vector3 exactUp)
{
Quaternion rotateZToUp = Quaternion.LookRotation(exactUp, -approximateForward);
Quaternion rotateYToZ = Quaternion.Euler(90f, 0f, 0f);
return rotateZToUp * rotateYToZ;
}
// Update is called once per frame
void Update()
{
Vector3 relativePos = target.position - transform.position;
// aligning axes of sensor to z axis and y axis respectively
Quaternion rotation1 = TurretLookRotation(relativePos, script.interpolatedNormalInWorldSpace);
// setting rotation of sensor to values entered in Inspector with respect to the above aligned axes
transform.rotation = rotation1 * rotation_edit;
}
}