본문 바로가기
코딩공부

유니티 캐릭터 움직이기

by 희망의 순례 2022. 10. 13.
728x90
반응형

4. 캐릭터 움직이기

    1. 먼저 세팅하기 : Visual Studio
    • 윈) Edit → Preferences → External Tools → Visual Studio Community 2019로 맞추기
    • 맥) Unity → Preferences → External Tools → Visual Studio for mac
    1. 캐릭터에 코딩을 더하는 법
    → 유니티에서는, 캐릭터가 코드를 갖고 있을 수 있음→ 가장 중요한 두 가지 함수가 있음. start (너는 태어날 때) / update (매 순간 이렇게 해라)
  • → 즉, 캐릭터에 코드를 입히는 것. "너는 태어날 때 여기서 태어나고, 매 순간 이렇게 작동해라"
    1. Script 만들기
    → Assets 우클릭 → Create → Folder (이름 Scripts) → Create → C# script (이름 rtan)
  • → C#은? Microsoft가 개발한 코딩 개발 언어. 희한하게 유니티에서만 주류로 쓰이고 있다.
    1. 캐릭터 좌우 움직임 코딩하기
      1. 캐릭터 오른쪽으로 이동하기
      → 아래와 같이 입력하고 캐릭터에 스크립트를 끌여다 놓기
      • [코드스니펫] 캐릭터 오른쪽으로 이동하기
      • void Update() { transform.position += new Vector3(0.05f, 0, 0); }
      void Update()
      {
          transform.position += new Vector3(0.05f, 0, 0);
      }
      
      → Play 버튼을 눌러보기 (캐릭터가 오른쪽으로 이동한다!)→ transform의 의미: 캐릭터의 위치와 중, position을 계속 바꿔달라는 것→ 트랜스폼 안의 포지션을, Vector3 방향으로 계속 더해주세요→ 위에 변수를 선언해서 이렇게 쓸 수도 있음!
    • float direction = 0.05f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.position += new Vector3(direction, 0, 0); }
    • → float 란? 소수점을 나타내는 자료형. 즉, 소수를 쓰고 싶으면 뒤에 f 를 붙여줘야 함
    • → transform.position += new Vector3(0.05f, 0, 0);
      1. 캐릭터가 벽에 닿으면 다른 방향을 보게 하기
      → 2-0) Debug.Log 로 위치 보기
      • [코드스니펫] Debug.log
      • Debug.Log(transform.position.x);
      Debug.Log(transform.position.x);
      
      C#
      • [코드스니펫] 760보다 클 때 다른 방향 보게 하기
      • float direction = 0.05f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (transform.position.x > 2.8f) { direction = -0.05f; } transform.position += new Vector3(direction, 0, 0); }
      float direction = 0.05f;
      
      // Start is called before the first frame update
      void Start()
      {
          
      }
      
      // Update is called once per frame
      void Update()
      {
          if (transform.position.x > 2.8f)
          {
              direction = -0.05f;
          }
          transform.position += new Vector3(direction, 0, 0);
      }
      
      → 2-2) 0보다 작을 때 다른 방향 보게 하기
    • → 2-1) 760보다 클 때 다른 방향 보게 하기

4. 캐릭터 움직이기

    1. 먼저 세팅하기 : Visual Studio
    • 윈) Edit → Preferences → External Tools → Visual Studio Community 2019로 맞추기
    • 맥) Unity → Preferences → External Tools → Visual Studio for mac
    1. 캐릭터에 코딩을 더하는 법
    → 유니티에서는, 캐릭터가 코드를 갖고 있을 수 있음→ 가장 중요한 두 가지 함수가 있음. start (너는 태어날 때) / update (매 순간 이렇게 해라)
  • → 즉, 캐릭터에 코드를 입히는 것. "너는 태어날 때 여기서 태어나고, 매 순간 이렇게 작동해라"
    1. Script 만들기
    → Assets 우클릭 → Create → Folder (이름 Scripts) → Create → C# script (이름 rtan)
  • → C#은? Microsoft가 개발한 코딩 개발 언어. 희한하게 유니티에서만 주류로 쓰이고 있다.
    1. 캐릭터 좌우 움직임 코딩하기
      1. 캐릭터 오른쪽으로 이동하기
      → 아래와 같이 입력하고 캐릭터에 스크립트를 끌여다 놓기
      • [코드스니펫] 캐릭터 오른쪽으로 이동하기
      • void Update() { transform.position += new Vector3(0.05f, 0, 0); }
      void Update()
      {
          transform.position += new Vector3(0.05f, 0, 0);
      }
      
      → Play 버튼을 눌러보기 (캐릭터가 오른쪽으로 이동한다!)→ transform의 의미: 캐릭터의 위치와 중, position을 계속 바꿔달라는 것→ 트랜스폼 안의 포지션을, Vector3 방향으로 계속 더해주세요→ 위에 변수를 선언해서 이렇게 쓸 수도 있음!
    • float direction = 0.05f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.position += new Vector3(direction, 0, 0); }
    • → float 란? 소수점을 나타내는 자료형. 즉, 소수를 쓰고 싶으면 뒤에 f 를 붙여줘야 함
    • → transform.position += new Vector3(0.05f, 0, 0);
      1. 캐릭터가 벽에 닿으면 다른 방향을 보게 하기
      → 2-0) Debug.Log 로 위치 보기
      • [코드스니펫] Debug.log
      • Debug.Log(transform.position.x);
      Debug.Log(transform.position.x);
      
      C#
      • [코드스니펫] 760보다 클 때 다른 방향 보게 하기
      • float direction = 0.05f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (transform.position.x > 2.8f) { direction = -0.05f; } transform.position += new Vector3(direction, 0, 0); }
      float direction = 0.05f;
      
      // Start is called before the first frame update
      void Start()
      {
          
      }
      
      // Update is called once per frame
      void Update()
      {
          if (transform.position.x > 2.8f)
          {
              direction = -0.05f;
          }
          transform.position += new Vector3(direction, 0, 0);
      }
      
      → 2-2) 0보다 작을 때 다른 방향 보게 하기
    • → 2-1) 760보다 클 때 다른 방향 보게 하기

4. 캐릭터 움직이기

    1. 먼저 세팅하기 : Visual Studio
    • 윈) Edit → Preferences → External Tools → Visual Studio Community 2019로 맞추기
    • 맥) Unity → Preferences → External Tools → Visual Studio for mac
    1. 캐릭터에 코딩을 더하는 법
    → 유니티에서는, 캐릭터가 코드를 갖고 있을 수 있음→ 가장 중요한 두 가지 함수가 있음. start (너는 태어날 때) / update (매 순간 이렇게 해라)
  • → 즉, 캐릭터에 코드를 입히는 것. "너는 태어날 때 여기서 태어나고, 매 순간 이렇게 작동해라"
    1. Script 만들기
    → Assets 우클릭 → Create → Folder (이름 Scripts) → Create → C# script (이름 rtan)
  • → C#은? Microsoft가 개발한 코딩 개발 언어. 희한하게 유니티에서만 주류로 쓰이고 있다.
    1. 캐릭터 좌우 움직임 코딩하기
      1. 캐릭터 오른쪽으로 이동하기
      → 아래와 같이 입력하고 캐릭터에 스크립트를 끌여다 놓기
      • [코드스니펫] 캐릭터 오른쪽으로 이동하기
      • void Update() { transform.position += new Vector3(0.05f, 0, 0); }
      void Update()
      {
          transform.position += new Vector3(0.05f, 0, 0);
      }
      
      → Play 버튼을 눌러보기 (캐릭터가 오른쪽으로 이동한다!)→ transform의 의미: 캐릭터의 위치와 중, position을 계속 바꿔달라는 것→ 트랜스폼 안의 포지션을, Vector3 방향으로 계속 더해주세요→ 위에 변수를 선언해서 이렇게 쓸 수도 있음!
    • float direction = 0.05f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.position += new Vector3(direction, 0, 0); }
    • → float 란? 소수점을 나타내는 자료형. 즉, 소수를 쓰고 싶으면 뒤에 f 를 붙여줘야 함
    • → transform.position += new Vector3(0.05f, 0, 0);
      1. 캐릭터가 벽에 닿으면 다른 방향을 보게 하기
      → 2-0) Debug.Log 로 위치 보기
      • [코드스니펫] Debug.log
      • Debug.Log(transform.position.x);
      Debug.Log(transform.position.x);
      
      C#
      • [코드스니펫] 760보다 클 때 다른 방향 보게 하기
      • float direction = 0.05f; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (transform.position.x > 2.8f) { direction = -0.05f; } transform.position += new Vector3(direction, 0, 0); }
      float direction = 0.05f;
      
      // Start is called before the first frame update
      void Start()
      {
          
      }
      
      // Update is called once per frame
      void Update()
      {
          if (transform.position.x > 2.8f)
          {
              direction = -0.05f;
          }
          transform.position += new Vector3(direction, 0, 0);
      }
      
      → 2-2) 0보다 작을 때 다른 방향 보게 하기
    • → 2-1) 760보다 클 때 다른 방향 보게 하기
728x90
반응형

댓글