유니티 심화

Vector3.normalized

다모아 2023. 8. 18. 10:39

normalized를 해주지 않으면 대각선 입력을 했을 때 동일한 속도를 보장하지 못한다.

좀 더 빨라짐 normalized로 정규화해주는거

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    private float moveSpeed = 1f;
    [SerializeField]
    private float turnSpeed = 1f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal"); // -1 ... 1
        float v = Input.GetAxis("Vertical"); // -1 ... 1
        float r = Input.GetAxis("Mouse X");
        //Debug.Log(r);
        Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h);

        //Debug.Log(new Vector3(h, 0, v));
        //Debug.LogFormat("{0}, {1}", moveDir, moveDir.normalized);

        DrawArrow.ForDebug(this.transform.position, moveDir.normalized);

        //this.transform.Translate(moveDir * this.moveSpeed * Time.deltaTime);
        this.transform.Translate(moveDir.normalized * this.moveSpeed * Time.deltaTime);

        //Debug.Log(Vector3.up * this.turnSpeed * Time.deltaTime * r);
        Vector3 angle = Vector3.up * this.turnSpeed * Time.deltaTime * r; //스피드 만큼 움직인다
        Vector3 noTime = Vector3.up * this.turnSpeed * r; // turSpeed만큼 움직인다
        Debug.LogFormat("{0}, {1}, {2}", r, angle, noTime);
        this.transform.Rotate(noTime);
    }

    private void OnDrawGizmos()
    {
        GizmosExtensions.DrawWireArc(this.transform.position, this.transform.forward, 360, 1);
    }
}