본문 바로가기
UnityTechnique

Vector3

by Mostlove 2023. 7. 28.
728x90
반응형

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

struct MyVector
{
    public float x;
    public float y;
    public float z;


    public float magnitude { get { return Mathf.Sqrt(x * x + y * y + z * z); } }
    public MyVector normalized { get { return new MyVector(x/magnitude,y/magnitude, z/magnitude); } }   
    public MyVector(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }

    public static MyVector operator + (MyVector a, MyVector b)
    {
        return new MyVector(a.x+ b.x,a.y + b.y,a.z+ b.z);
    }
    public static MyVector operator -(MyVector a, MyVector b)
    {
        return new MyVector(a.x - b.x, a.y - b.y, a.z - b.z);
    }
    public static MyVector operator *(MyVector a, float d)
    {
        return new MyVector(a.x - d, a.y - d, a.z - d);
    }
}

public class PlayerController : MonoBehaviour
{
    public float _speed = 10.0f;
    void Start()
    {
        MyVector to = new MyVector(10.0f, 0.0f, 0.0f);
        MyVector from = new MyVector(5.0f, 0.0f, 0.0f);
        MyVector dir = from - to;//(-5.0f, 0.0f, 0.0f);

        dir = dir.normalized;

        MyVector newPos = from + dir * _speed;
    }
    //GameObject (Player)
    //Transform
    //PlayerController(*)
    void Update()
    {
        //Local -> World
        //TransformDirection

        //World -> Local
        //InverseTransformdirection
        if (Input.GetKey(KeyCode.W))
            transform.Translate(Vector3.forward * Time.deltaTime);
        if (Input.GetKey(KeyCode.S))
            transform.Translate(Vector3.back * Time.deltaTime);
        if (Input.GetKey(KeyCode.D))
            transform.Translate(Vector3.right * Time.deltaTime);
        if (Input.GetKey(KeyCode.A))
            transform.Translate(Vector3.left * Time.deltaTime);
        //transform
    }
}

반응형