티스토리 뷰

프로그래밍적으론 별건 아니지만

게임의 시야를 넓히기 위해선 하나의 유닛이 아니라 여러 유닛을 컨트롤 해봐야 합니다.


대표적으로 전략시뮬레이션이 있고요.

축구 게임도 마찬 가지겠죠.


1인칭이라는 시야에서 벗어나

다자 주인공에 포인트를 두는 것은 중요한 일인 것 같습니다.


그런 김에 한번 간단하게 다자 컨트롤을 만들어봤습니다.

인풋은 마우스 클릭이고요.

클릭된 지점에 보라색연기가 뿜뿜합니다.


영상부터 첨부할게요





스크립트는 두 개뿐입니다.


각 히어로에 붙을 히어로 스크립트와

인풋을 받고 여러 신호를 전달할 Ex00 이라는 스크립트입니다.


올려볼게요.


Ex00입니다.


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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Ex00 : MonoBehaviour {
    [SerializeField]
    Transform target;
    [SerializeField]
    ParticleSystem ps;
    Hero[] hero;
    void Start () {
        hero = GameObject.FindObjectsOfType<Hero>();
    }
    int selectedNum;
    void Update () {
        if (Input.GetMouseButtonUp(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Physics.Raycast(ray, out hit);
            if(hit.collider != null)
            {
                ps.transform.position = hit.point;
                ps.Play();
                hero[selectedNum].Move(hit.point);
            }
        }    
    }
 
    private void OnGUI()
    {
        if (GUILayout.Button("1번"))
        {
            selectedNum = 0;
        }else if (GUILayout.Button("2번"))
        {
            selectedNum = 1;
        }
        else if (GUILayout.Button("3번"))
        {
            selectedNum = 2;
        }
        else if (GUILayout.Button("4번"))
        {
            selectedNum = 3;
        }
        GUI.Label(new Rect(502510030), (selectedNum+1)+"번 선택중");
    }
}
 
cs


히어로입니다.


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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Hero : MonoBehaviour {
 
    Coroutine moveCoroutine;
    public float speed = 2;
    Animation anim;
    private void Start()
    {
        anim = transform.GetChild(0).GetComponent<Animation>();
    }
    public void Move(Vector3 targetPos)
    {
        if(moveCoroutine != null)
        {
            StopCoroutine(moveCoroutine);
        }
        moveCoroutine = StartCoroutine(MoveImpl(targetPos));
    }
    IEnumerator MoveImpl(Vector3 targetPos)
    {
        anim.Play("run@loop");
        targetPos.y = 0;
        while (true)
        {
            Vector3 v = targetPos - transform.position;
            float angle = Mathf.Atan2(v.x, v.z) * Mathf.Rad2Deg;
            transform.rotation = Quaternion.Euler(0, angle, 0);
            transform.Translate(00, speed * Time.deltaTime);
            if(v.sqrMagnitude < 0.1f)
            {
                anim.Play("idle@loop");
                break;
            }
            yield return null;
        }
    }
}
 
cs


어려운 건 하나도 없는 것 같아요. 쉬운 것만 있어요~~~

질문 있으시면 댓글 달아주세요.

댓글
최근에 올라온 글
Total
Today
Yesterday
TAG
more
«   2024/10   »
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