반응형
1. 키보드로 이동하는 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
// public으로 설정하면 툴에서 할당이 가능하다.
public float speed;
private Rigidbody rb;
// 시작될때 한번만 호출된다
void Start(){
// AddForce를 이용하기위한
this.rb = GetComponent<Rigidbody> ();
}
// 물리 효과
void FixedUpdate(){
// 키보드로부터 입력된 좌표
float moveHorizotal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
// x, y, z
Vector3 movement = new Vector3 (moveHorizotal, 0.0f, moveVertical);
// 물리력을 가한다. (이동 효과)
rb.AddForce (movement * speed);
}
}
반응형
'Programming > Unity' 카테고리의 다른 글
[Unity] 회전하는 오브젝트 스크립트 (0) | 2017.10.09 |
---|---|
[Unity] 추적 카메라 스크립트 (0) | 2017.10.09 |