반응형


플레이어 비행기를 움직이는 스크립트를 짜보겠습니다



스크립트 폴더에 스크립트를 생성하고 이름을 Player라고 합니다



스크립트를 열고  그림과 같이 스크립트를 짭니다 

여기서 Horizontal은 유니티 엔진에서 제공하는 좌우 input  GetAxis이고 좌표 X에 -1과 1을 대입하게 됩니다



using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class Player : MonoBehaviour

{

    public float moveSpeed = 0.5f;


        


// Use this for initialization

void Start ()

    {

}

void MoveControl()

    {

        float moveX = moveSpeed * Time.deltaTime * Input.GetAxis("Horizontal");

        transform.Translate(moveX, 0, 0);

    }

// Update is called once per frame

void Update ()

    {

        MoveControl();


    }

}



Player 오브젝트를 선택하고 스크립트를 드래그하여 붙힙니다 그리고

키보드 A 와 D 키 와 키보드 좌우키를 움직이면 비행기가 움직입니다. 



반응형

+ Recent posts