반응형

상하좌우로 방향을 전환하는 컨트롤러 스크립트를 만들어 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Controller : MonoBehaviour
{
    private Rigidbody m_rigidbody;
    public float speed = 3f;
 
    // Start is called before the first frame update
    void Start()
    {
        m_rigidbody = GetComponent<Rigidbody>();
    }
    
    // Update is called once per frame
    void Update()
    {
        if (GameManager.instance.isGameover)
            return;
 
        float xInput = Input.GetAxis("Horizontal");
        float zInput = Input.GetAxis("Vertical");
 
        float xSpeed = xInput * speed;
        float zSpeed = zInput * speed;
 
        Vector3 newVelocity = new Vector3(xSpeed, 0f, zSpeed);
        m_rigidbody.velocity = newVelocity;
        
        if (xInput > 0)
        {
            transform.rotation = Quaternion.Euler(new Vector3(0f, 180f, 0f));
        }
        else if (xInput < 0)
        {
            transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 0f));
        }
        else if (zInput > 0)
        {
            transform.rotation = Quaternion.Euler(new Vector3(0f, 90f, 0f));
        }
        else if (zInput < 0)
        {
            transform.rotation = Quaternion.Euler(new Vector3(0f, 270f, 0f));
        }
    }
}
 

 

팩맨이 움직이는 방향으로 회전한다.

 

반응형
Posted by J-sean
:
반응형

텍스트 파일로 간단한 맵을 생성해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Spawner : MonoBehaviour
{
    public GameObject[] objects;    
 
    // Start is called before the first frame update
    void Start()
    {
        string path = Application.dataPath + "/Map.txt";
        string[] map = System.IO.File.ReadAllLines(path);
 
        if (map.Length > 0)
        {
            for (int i=0; i<map.Length; i++)
            {
                for (int j=0; j<map[i].Length; j++)
                {
                    if (map[i][j] != ' ')
                        Instantiate(objects[int.Parse(map[i][j].ToString())], new Vector3(-(i * 1), 0-(j * 1)), Quaternion.identity);
                        //Instantiate(objects[(int)char.GetNumericValue(map[i][j])], new Vector3(-(i * 1), 0, -(j * 1)), Quaternion.identity);
                    
                }
            }
        }
    }
}
 

위 스크립를 작성하고 Empty Object에 추가한다.

Map.txt
0.00MB

Assets 폴더에 Map.txt 파일을 준비한다.

0: 큐브, 1: 스피어

 

스크립트가 추가된 오브젝트의 Inspector 창에서 Objects에 미리 준비한 Cube, Sphere 오브젝트를 추가한다.

 

간단한 미로맵이 완성 되었다.

 

반응형
Posted by J-sean
:
반응형

텍스트 파일을 읽고 내용을 출력해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TextReader : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        string path = Application.dataPath;    // D:/UnityTest/My project/Assets
        path += "/Text/Text.txt";    // D:/UnityTest/My project/Assets/Text/Text.txt
 
        /* Text 파일 내용
        First text line.
        Second text line.
        Third text line.
        Fourth text line.
        Fifth text line.
        1,2,3,4,5
        6,7,8,9,10
        */
 
        string[] contents = System.IO.File.ReadAllLines(path);
        if (contents.Length > 0)
        {
            for (int i=0; i<contents.Length; i++)
            {
                if (i < 5)
                {
                    Debug.Log(contents[i]);
                }
                else // 숫자 텍스트
                {
                    string[] txtArr = contents[i].Split(',');
                    int[] numArr = new int[txtArr.Length];
                    int total = 0;
 
                    for (int j=0; j<txtArr.Length; j++)
                    {
                        numArr[j] = int.Parse(txtArr[j]);
                        total += numArr[j];
                    }
 
                    Debug.Log(total);
                }
            }
        }
 
        string content = System.IO.File.ReadAllText(path);
        Debug.Log(content);
    }
}
 

 

 

반응형
Posted by J-sean
: