foliran
foliran

Reputation: 75

Unity 3d GO orientation

I look for the best way to detect, what site of an GO (cube) is facing upwards. My research led me to the Dot-product. I know what I want to do, but I guess my c# skills are too bad..

Basically I just want to find the Dot-product for example for the x-rotation. And if its between a value of 0,9 to 1 one site is facing upwards, for -0,9 to -1 the other Site and so on for all axes.

Upvotes: 0

Views: 35

Answers (1)

Eric
Eric

Reputation: 1775

In the Unity's documentation you have the following example. You just have to tweak it a little bit in order to achieve what you need.

// detects if other transform is behind this object

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Transform other;

    void Update()
    {
        if (other)
        {
            Vector3 forward = transform.TransformDirection(Vector3.forward);
            Vector3 toOther = other.position - transform.position;

            if (Vector3.Dot(forward, toOther) < 0)
            {
                print("The other transform is behind me!");
            }
        }
    }
}

Upvotes: 1

Related Questions