점프게임 소스
게임소스
http://freebox.myqnapcloud.com:8080/share.cgi?ssid=0mja5JC
====== Cplayer =============
http://freebox.myqnapcloud.com:8080/share.cgi?ssid=0mja5JC
====== Cplayer =============
using UnityEngine; using System.Collections; using UnityEngine.UI; public class Cplayer : MonoBehaviour { // 시선처리 bool _isRighrDir; Vector3 _scale; // 바닥 처리 bool isGround; //기본값 bool isGroundFast; // 빠르게 bool isGroundSlow; // 느리게 //더블점프 bool _oneJump = false; bool _dubbleJump = false; public float jumpPower; //속도 public float playerSpeed; // 기본값 public float playerSpeedFast; // 빠르게 public float playerSpeedSlow; // 느리게 public float hMove; //플레이어 무빙 바닥 이동따라감 Vector2 _playerPos; GameObject newParent; Rigidbody2D _playerRB; //애니메이션 Animator _myAnim; // 그라운드 체크 public Transform GroundChecker1; public Transform GroundChecker2; void Start() { _myAnim = GetComponent<Animator>(); _playerRB = GetComponent<Rigidbody2D>(); //newParent = GameObject.Find("ground_grass"); //transform.parent = newParent.transform; } void Update() { Move(); GroundCheck(); //점프체크 JumpAnim(); JumpControl(); } void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "Movefloor1") { transform.parent = col.gameObject.transform; } else { transform.parent = null; } } public void PosX(float posx) { posx -= _playerPos.x; _playerPos = transform.position; _playerPos.x += posx; transform.position = _playerPos; } void GroundCheck() { isGround = (Physics2D.OverlapPoint(GroundChecker1.position, 1 << LayerMask.NameToLayer("Ground")) || Physics2D.OverlapPoint(GroundChecker2.position, 1 << LayerMask.NameToLayer("Ground"))); isGroundFast = (Physics2D.OverlapPoint(GroundChecker1.position, 1 << LayerMask.NameToLayer("GroundFast")) || Physics2D.OverlapPoint(GroundChecker2.position, 1 << LayerMask.NameToLayer("GroundFast"))); isGroundSlow = (Physics2D.OverlapPoint(GroundChecker1.position, 1 << LayerMask.NameToLayer("GroundSlow")) || Physics2D.OverlapPoint(GroundChecker2.position, 1 << LayerMask.NameToLayer("GroundSlow"))); //Debug.Log(isGround); } public void Move() { hMove = Input.GetAxis("Horizontal"); if (hMove != 0) _myAnim.SetBool("walk", true); else _myAnim.SetBool("walk", false); //회전방향 if ((hMove < 0 && !_isRighrDir) || (hMove > 0 && _isRighrDir)) { _scale = transform.localScale; _scale.x *= -1; transform.localScale = _scale; _isRighrDir = !_isRighrDir; } //그라운드에 따른 속도 제어 if (isGround) _playerRB.velocity = new Vector2(hMove * playerSpeed, _playerRB.velocity.y); else if (isGroundFast) _playerRB.velocity = new Vector2(hMove * playerSpeedFast, _playerRB.velocity.y); else if (isGroundSlow) _playerRB.velocity = new Vector2(hMove * playerSpeedSlow, _playerRB.velocity.y); } public void JumpControl() { //if (isGround || isGroundFast || isGroundSlow) _oneJump = true; // 첫번째 점프 if (isGround || isGroundFast || isGroundSlow) { if (Input.GetKeyDown(KeyCode.Space)) { _oneJump = true; Jump(); } } else if ((_oneJump) && (!isGround || !isGroundFast || !isGroundSlow)) { if (Input.GetKeyDown(KeyCode.Space)) { _dubbleJump = true; Jump(); _oneJump = false; } } _dubbleJump = false; } public void Jump() { _playerRB.velocity = new Vector2(_playerRB.velocity.x, 0f); _playerRB.AddForce(new Vector2(0f, jumpPower)); //_playerRB.AddForce(transform.up * jumpPower); } public void JumpAnim() { if (isGround || isGroundFast || isGroundSlow) { _myAnim.SetBool("jump", false); } else { _myAnim.SetBool("jump", true); _myAnim.SetBool("walk", false); } } }
====== [ MovingFloor ] ======
using UnityEngine; using System.Collections; public class MovingFloor : MonoBehaviour { public float speed; public bool dir; Vector2 pos; void Start() { } void Update() { Move(); } public void Move() { pos = transform.position; // 정방향 if (dir) { if (pos.x >= 3.6f) { pos.x = -3.6f; transform.position = pos; } transform.Translate(Vector2.right * speed * Time.deltaTime); } //반대방향 else { if (pos.x <= -3.6f) { pos.x = 3.6f; transform.position = pos; } transform.Translate(-Vector2.right * speed * Time.deltaTime); } } }====[ MovingFloor_2Point ]====
using UnityEngine; using System.Collections; public class MovingFloor_2Point : MonoBehaviour { public Transform point1; public Transform point2; public float smoothTime; float Yvelocity = 0.0f; float Xvelocity = 0.0f; public bool _isDir = true; Vector2 pos; // Use this for initialization void Start() { } // Update is called once per frame void Update() { Move(); DirSwitch(); } void DirSwitch() { pos = transform.position; if (pos.x >= point1.position.x) { _isDir = false; } else if (pos.x <= point2.position.x) { _isDir = true; }} void Move() { if (_isDir) { float newPositionY = Mathf.SmoothDamp(transform.position.y, point1.position.y, ref Yvelocity, smoothTime); float newPositionX = Mathf.SmoothDamp(transform.position.x, point1.position.x + 0.1f, ref Xvelocity, smoothTime); transform.position = new Vector3(newPositionX, newPositionY, transform.position.z); } else { float newPositionY = Mathf.SmoothDamp(transform.position.y, point2.position.y, ref Yvelocity, smoothTime); float newPositionX = Mathf.SmoothDamp(transform.position.x, point2.position.x - 0.1f, ref Xvelocity, smoothTime); transform.position = new Vector3(newPositionX, newPositionY, transform.position.z); } } }=====[ MovingFloor_UpDown ]======
using UnityEngine; using System.Collections; public class MovingFloor_UpDown : MonoBehaviour { //public float _startPos; // 이동거리 public float _endPos; public float _speed; // 방향설정 public bool _myDir; // 오브젝트 포지션 Vector2 _myPos; Vector2 _startPos; float sPos; public float smoothTime = 0.3f; private float yVelocity = 0.0f; // Use this for initialization void Start () { _startPos = transform.position; sPos = _startPos.y; Debug.Log(sPos - _endPos); _myDir = true; } // Update is called once per frame void Update () { Move(); UpDownSwitch(); } public void UpDownSwitch() { _myPos = transform.position; if (_myPos.y >= (sPos + _endPos)) { _myDir = false; } if (_myPos.y <= (sPos - _endPos)) { _myDir = true; } } public void Move() { if (_myDir) { float newPostion = Mathf.SmoothDamp(transform.position.y, (sPos + _endPos)+0.1f ,ref yVelocity, smoothTime ); transform.position = new Vector3(transform.position.x , newPostion ,transform.position.z); } else { float newPosition = Mathf.SmoothDamp(transform.position.y, (sPos - _endPos)-0.1f, ref yVelocity, smoothTime); transform.position = new Vector3(transform.position.x, newPosition, transform.position.z); } } }
댓글
댓글 쓰기