0
\$\begingroup\$

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;

}

}

\$\endgroup\$
2
  • \$\begingroup\$ It looks to me like you might want a variant of the standard LookRotation as defined here, where it points your up vector exactly in your normal direction, and then aligns the forward as close as it can to your chosen facing direction, subject to that constraint. \$\endgroup\$ – DMGregory May 14 '20 at 17:03
  • \$\begingroup\$ @DMGregory thank you for the suggestion. I have edited the code (in the main post). It still will not rotate. I am probably giving the forward vector wrong. Does relativePos not tell the cube to "face" the character? The objective is to rotate the sensor on the surface of the skin (that would be around one axis). An example in real life would be wearing a watch that could be rotated around one axis in either direction. And I want control of this rotation in my Inspector using a public Quaternion variable. I need to figure out what forward vector to pass the LookRotation() function. \$\endgroup\$ – poojiiee May 15 '20 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.