Transform.TransformDirection

Unity3D 2021. 10. 19. 23:41
반응형

 

https://youtu.be/tetIRfxY3ro

Cube.cs
0.00MB

 

DrawArrow.cs
0.00MB
Test.cs
0.00MB

using UnityEngine;
using System.Collections;

public static class DrawArrow
{
    public static void ForGizmo(Vector3 pos, Vector3 direction, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f)
    {
        Gizmos.DrawRay(pos, direction);

        Vector3 right = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 + arrowHeadAngle, 0) * new Vector3(0, 0, 1);
        Vector3 left = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 - arrowHeadAngle, 0) * new Vector3(0, 0, 1);
        Gizmos.DrawRay(pos + direction, right * arrowHeadLength);
        Gizmos.DrawRay(pos + direction, left * arrowHeadLength);
    }

    public static void ForGizmo(Vector3 pos, Vector3 direction, Color color, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f)
    {
        Gizmos.color = color;
        Gizmos.DrawRay(pos, direction);

        Vector3 right = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 + arrowHeadAngle, 0) * new Vector3(0, 0, 1);
        Vector3 left = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 - arrowHeadAngle, 0) * new Vector3(0, 0, 1);
        Gizmos.DrawRay(pos + direction, right * arrowHeadLength);
        Gizmos.DrawRay(pos + direction, left * arrowHeadLength);
    }

    public static void ForDebug(Color color, Vector3 pos, Vector3 direction, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f)
    {
        //Debug.DrawRay(pos, direction);
        Debug.DrawRay(pos, direction, color, 3);

        Vector3 right = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 + arrowHeadAngle, 0) * new Vector3(0, 0, 1);
        Vector3 left = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 - arrowHeadAngle, 0) * new Vector3(0, 0, 1);
        //Debug.DrawRay(pos + direction, right * arrowHeadLength);
        //Debug.DrawRay(pos + direction, left * arrowHeadLength);

        Debug.DrawRay(pos + direction, right * arrowHeadLength, color, 3);
        Debug.DrawRay(pos + direction, left * arrowHeadLength, color, 3);

    }
    public static void ForDebug(Vector3 pos, Vector3 direction, Color color, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20.0f)
    {
        Debug.DrawRay(pos, direction, color);

        Vector3 right = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 + arrowHeadAngle, 0) * new Vector3(0, 0, 1);
        Vector3 left = Quaternion.LookRotation(direction) * Quaternion.Euler(0, 180 - arrowHeadAngle, 0) * new Vector3(0, 0, 1);
        Debug.DrawRay(pos + direction, right * arrowHeadLength, color);
        Debug.DrawRay(pos + direction, left * arrowHeadLength, color);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public Button btn;
    public Transform player;
    public Transform localTrans;
    public Transform friend;
    // Start is called before the first frame update
    void Start()
    {
        this.btn.onClick.AddListener(() => {
            //매개변수로 받은 벡터를 transform의 로컬 기준에서 월드 기준으로 변환 
            //var dir = this.player.transform.TransformDirection(Vector3.forward);
            //Debug.LogFormat("<color=red>{0}</color>", dir);

            ////var dir = this.player.transform.TransformDirection(localTrans.localPosition);
            //var dir2 = player.transform.TransformDirection(this.player.forward);
            //Debug.LogFormat("<color=yellow>{0}</color>", dir2);
            //DrawArrow.ForDebug(this.player.transform.position, this.player.forward, 0.1f, 30);
            DrawArrow.ForDebug(Color.red, this.player.transform.position, Vector3.forward, 0.1f, 30);
            Debug.Log(this.player.forward);
            DrawArrow.ForDebug(Color.yellow, this.player.transform.position, this.player.forward, 0.1f, 30);
            //DrawArrow.ForDebug(Vector3.zero, dir, 0.1f, 30);
        });
    }

    // Update is called once per frame
    void Update()
    {
        //this.friend.rotation = Quaternion.LookRotation(player.transform.TransformDirection(Vector3.forward));
        this.friend.rotation = Quaternion.LookRotation(player.transform.TransformDirection(this.player.forward));
    }
}

https://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html

반응형

'Unity3D' 카테고리의 다른 글

논스톱 나이츠 라이크 프로토타입 만들기 - 프로젝트 세팅과 리소스 준비  (0) 2021.10.21
Transform.forward, Vector3.forward  (0) 2021.10.20
AR Foundation  (0) 2021.07.30
Vuforia AR  (0) 2021.07.30
[GVR] 360VR  (0) 2021.07.30
: