2D 캐릭터 이동과 점프 하는 소스
Player.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 42 43 44 45 46 47 48 49 50 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { public float speed = 5f; //이동속도 public float jump_speed = 5f; //점프 높이 private Rigidbody2D rd; //점프 하기 위해 rigidbody 가져옴 private bool facingRight = true ; //캐릭터 이동회전 // Use this for initialization void Start () { rd = GetComponent<rigidbody2d>(); } // Update is called once per frame void FixedUpdate () { float hor = Input.GetAxis( "Horizontal" ); //이동 : 1 = 오른쪽, -1 = 왼쪽 transform.Translate(Vector3.right * speed * hor * Time.deltaTime); if (Input.GetAxis( "Horizontal" ) > 0.5f || Input.GetAxis( "Horizontal" ) < -0.5f) { // 만약 왼쪽 혹은 오른쪽 이동 중 0.5f 이상인 경우 if (Input.GetAxis( "Horizontal" ) > 0.5f && !facingRight) //facingRight 가 false이면서 오른쪽 이동키 누른 경우. { Flip(); facingRight = true ; } else if (Input.GetAxis( "Horizontal" ) < -0.5f && facingRight) // gacingRight 가 true이면서 왼쪽 이동키 누른 경우. { Flip(); facingRight = false ; } } if (Input.GetKey(KeyCode.Space)) //스페이스바 키 누를 시 점프 { rd.velocity = Vector3.up * jump_speed; } } void Flip() { facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; // 1 = 오른쪽 방향, -1 = 왼쪽방향 transform.localScale = theScale; } } |
'시바 | Unity(유니티) 5.x' 카테고리의 다른 글
카메라 추적 상하 좌우 따라 움직이기 (0) | 2018.03.20 |
---|---|
카메라 추적 마우스 좌우만 움직이기 (0) | 2018.03.20 |
Unity 스크립트(script)에서 오브젝트(GameObejct) 생성하기 (0) | 2018.03.14 |
Unity getkey 이벤트 차이점 (0) | 2018.03.14 |
[Unity] Vector3에 대한 이해 (0) | 2018.03.09 |
[Unity] Mathf.clamp함수란? (0) | 2018.03.09 |
(Unity) 유니티 프로젝트 폴더구조 (0) | 2018.03.06 |
(Unity) 유니티 캐릭터 이동 +회전 만들기 (0) | 2018.03.05 |