Unity/3D

Vector3 사용 연습

s0002 2023. 1. 30. 16:54

Vector3 Documentation

https://docs.unity3d.com/kr/530/ScriptReference/Vector3.html

 

UnityEngine.Vector3 - Unity 스크립팅 API

Representation of 3D vectors and points.

docs.unity3d.com

Vector3는 Vector 클래스의 구조체 형식 멤버변수이다

 


GameObject masakiGo를 회전 및 이동/서서히 멈추게 하기

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

public class App : MonoBehaviour
{
    public GameObject masakiGo;
    Vector3 playerPos;
    public float speed;
    public float rotSpeed;

    // Start is called before the first frame update
    void Start()
    {
        //GamaeObject masakiGo의 위치 출력
        playerPos = this.masakiGo.transform.position;
        Debug.LogFormat("x:{0},y:{1},z:{2}",playerPos.x,playerPos.y,playerPos.z);           
    }

    // Update is called once per frame
    void Update()
    {
        //마우스 클릭 감지
        //마우스 왼쪽 버튼 누른 순간 값 할당
        if (Input.GetMouseButtonDown(0))
        {
            this.speed = 0.01f;          
        }
        //마우스 오른쪽 버튼 누른 순간 값 할당
        if (Input.GetMouseButtonDown(1))
        {
            this.rotSpeed = 1.0f;
        }

        masakiGo.transform.Translate(this.speed, 0, 0, Space.World);
        masakiGo.transform.Rotate(0,this.rotSpeed , 0);
        
        //감쇠계수 사용해서 회전, 이동 멈추기
        this.rotSpeed *= 0.97f;
        if (this.rotSpeed<0.001f)
        {
            this.rotSpeed = 0;
        }

        this.speed *= 0.97f;
        if (this.speed < 0.001f)
        {
            this.speed = 0;
        }

        //playerPos.z += 0.01f;//앞으로 이동
        //playerPos.y+=1.0f; //위로 이동
        //playerPos.x+=1.0f; //오른쪽으로 이동

        //Vector3 구조체 형식이므로 변경된 playerPos 값을 넣어줘야 함
        //this.masakiGo.transform.position = playerPos;
    }
}