본문 바로가기
KDT/유니티 기초

23/08/01 SwipeCar

by 잰쟁 2023. 8. 1.
728x90

 

using System.Collections;
using System.Collections.Generic;
using UnityEditor.Tilemaps;
using UnityEngine;

public class CarController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //매프레임마다 호출됨
        //마우스 왼쪽 버튼을 눌렀다면
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("왼쪽버튼 눌림!");

            //원점으로 게임 오브젝트를 이동하자
            Debug.Log(this);    //carcontroller클래스이 인스턴스
            Debug.Log(this.gameObject); //carcontroller컴포넌트가 붙어있는 게임오브젝트 instance
            //이동하자: 위치를 변경하자
            //위치: 게임오브젝트 -> 맴버 transform -> 맴버 position -> 맴버 x,y,z
            //원점: 0,0,0

            //this.gameObject.transform.position.x = 0;  (x)
            //구조체맴버(vector) 싱글로 할당 불가  ==> 구조체맴버는 반드시 모두 채워져야함

            //새로운 위치: new Vector3(0, 0, 0);

            this.gameObject.transform.position = new Vector3(0, 0, 0);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Tilemaps;
using UnityEngine;

public class CarController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //매프레임마다 호출됨
        //마우스 왼쪽 버튼을 눌렀다면
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("왼쪽버튼 눌림!");
            //이동

            //벡터
            //-이동벡터 : 위치
            //-물리벡터 : 크기와 방향

            //성분 Vector3(x,y,z)
            //Vector3 연산

            //이동한다 x축으로 1만큼
            //this.gameObject.transform.position += new Vector3(1, 0, 0);
            //this.gameObject.transform.Translate(1, 0, 0);
            //this.gameObject.transform
            this.transform.Translate(1, 0, 0);
        }
    }
}

버튼 눌렀을때 0.2만큼 이동한다

Translate: 현재 좌표의 상대적인 이동 값을 나타냄

나의 좌표에 인수로 전달되는 x,y,z의 값을 더한다

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Tilemaps;
using UnityEngine;

public class CarController : MonoBehaviour
{
    float moveSpeed = 0;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //매프레임마다 호출됨
        //마우스 왼쪽 버튼을 눌렀다면
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("왼쪽버튼 눌림!");
            this.moveSpeed = 0.2f;
        }
        //매프레임마다 1유닛씩 x축으로 이동
        this.transform.Translate(this.moveSpeed, 0, 0);
    }
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Tilemaps;
using UnityEngine;

public class CarController : MonoBehaviour
{
    float moveSpeed = 0;
    float dampingCoefficient = 0.98f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //매프레임마다 호출됨
        //마우스 왼쪽 버튼을 눌렀다면
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("왼쪽버튼 눌림!");
            this.moveSpeed = 0.2f;
        }
        //매프레임마다 1유닛씩 x축으로 이동
        this.transform.Translate(this.moveSpeed, 0, 0);

        //감쇠한다
        this.moveSpeed *= dampingCoefficient; //0.2 * 0.98 = 0.196
        //...
        //..
        //.
        //0과 가까워짐
    }
}