이번 시간에는 스크립트를 작성해서 자동차를 움직여 보도록 하게씁니다 ^^!

 

움직이는 방식은 스와이프를 통해 움직이겠습니다!

 

C# 스크립트를 하나 만들어주시고 이름은 SisterControler

 

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class SisterControler : MonoBehaviour
{
 
    float speed = 0;
    Vector2 startPos;
 
    void Start()
    {
 
    }
 
    void Update()
    {
        //스와이프의 길이를 구한다
        if (Input.GetMouseButtonDown(0))
        {
            this.startPos = Input.mousePosition;//시작 좌표저장
        }
        else if(Input.GetMouseButtonUp(0))
        {
            Vector2 endPos = Input.mousePosition;//끝 좌표저장
            float swipeLength = (endPos.x - this.startPos.x);//끝 x좌표-시작 x좌표
 
            this.speed = swipeLength / 500.0f;//스와이프 길이를 작게 나누기.
 
        }
 
        transform.Translate(this.speed, 0, 0);
        this.speed *= 0.98f;
    }
}
 
cs

 

 

 

 

c#스크립트를 Sister에게 적용합니다.

 

 

 

 

플레이 해보세요~

 

 

 

 

참고도서 : 유니티5 교과서

 

 

+ Recent posts