[런게임] 유니티 3D로 러너 게임 만들기 - 점프와 점프 실패

|

점프와 점프 실패

우리는 길을 가지고 있다, 우리는 이 게임을 좀더 업그레이드 할수있다. 우리는 유니티의 물리 엔진을 사용하여 점프를 구현할 것이다. 점프와 떨어지는 것을 구현할려면 콜라이더가 필요하다.  Rigidbody 컴포넌트를 Runner 에 추가하자(Component / Physics / Rigidbody). 우리는 특정방향으로 회전하거나 이동하는것을 원하지 않으므로, constrain 값을 freezing Z position 과모든 rotation axes에 체크하자.

길위를 움직일때 어떠한 physic material 이 일어납니다.  physic material 을 생성하고 모든 필드를 0으로 설정하고 combine options은 둘다 max로 설정합니다.

Runner PMat를 Runner folder 폴더에 넣고, Runner의 박스 콜라이더에 집어넣는다.

Runner 의 위치를 (0, 2, 0)로 설정하고 길 위로 떨어트린다. 그리고 플레이 버튼을 눌러서 어떤일이 일어나는지 봐라!

runner materialgame
Runner with physics.

Runner는 오른쪽으로 이동하다 보면 구멍에 떨어지게 됩니다. 길을 빠르게 지나가도 다시 떨어지게 되있습니다. 하지만 길의 옆면에 충돌이 발생할 경우 이상한 일이 일어납니다. 

Update

함수에서 위치를 변경하기 때문입니다. 우리는 물리엔진을 그대로 두고 forces를 적용해야 됩니다.

Update 에서  Translate 을 부르는걸 제거합니다((Runner 스크립트). 대신에 우리는 유니티 collision  이벤트 메서드를 부릅니다(OnCollisionEnter ,OnCollisionExit ) – 길위에 부딪히거나 떨어질때 호출됩니다. 길위에 터치되면 우리는 가속도가 적용되어 속도가 빨라집니다.

acceleration를 5로 설정합시다.

using UnityEngine;

public class Runner : MonoBehaviour {

	public static float distanceTraveled;

	public float acceleration;

	private bool touchingPlatform;

	void Update () {
		distanceTraveled = transform.localPosition.x;
	}

	void FixedUpdate () {
		if(touchingPlatform){
			rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
		}
	}

	void OnCollisionEnter () {
		touchingPlatform = true;
	}

	void OnCollisionExit () {
		touchingPlatform = false;
	}
}

이 작업을 하기 전에 한가지 더 할 일이 있습니다. Runner PMat을 복사하고, Platform Regular PMat이라고 지정해서  Platform 폴더에 집어넣습니다. friction 값은 0.05로 설정하고 physic material을 Platform prefab에 드래그합니다.

우리의 길은 약간의 마찰력을  제공하지만 Runner은 움직일수 있는 충분한 가속도가 있습니다.

acceleration platformphysic material
Acceleration and regular platform.
Runner의 점프를 만들어 봅시다.  우리는 플레이어에 input을 붙여야 됩니다. Unity는 기본적으로 점프 액션이 세팅 되어있습니다. Edit / Project Settings / Input 에서 찾을수 있으며, 스페이스바를 누르면 작동 됩니다. 우리는 이것을 사용할 것이며, Alt Positive Button 필드에 x를 집어넣습니다.
Jump input configuration.
Runner

 에 점프 관련 변수를 더합니다. 우리는 수평과 수직을 조작할수 있도록 float대신 Vector3 클래스를 사용할 예정입니다. 필드의 값을 (1, 7, 0)로 설정합니다.

  Update 함수에 아래 내용을 추가하면 Runner이 점프버튼을 누를시 점프하게 될 것입니다.

	public Vector3 jumpVelocity;

	void Update () {
		if(touchingPlatform && Input.GetButtonDown("Jump")){
			rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
		}
		distanceTraveled = transform.localPosition.x;
	}
settings game
Runner jumping.
우리는 점프를 할수있습니다! 하지만, 만약 길의 양 옆에 위치하게 되면(길과 계속 터치하고 있으면) 여러번 점프가 가능합니다. 이것을 막으려면 아래 코드를 추가합니다.
	void Update () {
		if(touchingPlatform && Input.GetButtonDown("Jump")){
			rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
			touchingPlatform = false;

} distanceTraveled = transform.localPosition.x; }


Trackback 0 And Comment 0

[런게임] 유니티 3D로 러너 게임 만들기 - 플랫폼(길) 생성하기

|

길 생성하기

길을 생성하는 방법은 스카이라인을 생성하는 방법에서 약간만 다르다. 길은 길과 길 사이의 거리와 높이가 랜덤하게 배치된다. 또한 우리는 스카이라인이 보이게 길의 위치나 고도를 제한할 필욛 있다. 만약 길이 화면 밖에 있으면 우리는 그것을 다시 사용 해야 된다.

Platform이라는 폴더를 생성한다PlatformManager라는 C# script를 생성한다 . 그리고 SkylineManager 코드를 복사한다. 그리고 아래처럼 몇개의 코드만 고친다.

using UnityEngine;
using System.Collections.Generic;

public class PlatformManager : MonoBehaviour {

	public Transform prefab;
	public int numberOfObjects;
	public float recycleOffset;
	public Vector3 startPosition;
	public Vector3 minSize, maxSize, minGap, maxGap;
	public float minY, maxY;

	private Vector3 nextPosition;
	private Queue<Transform> objectQueue;

	void Start () {
		objectQueue = new Queue<Transform>(numberOfObjects);
		for(int i = 0; i < numberOfObjects; i++){
			objectQueue.Enqueue((Transform)Instantiate(prefab));
		}
		nextPosition = startPosition;
		for(int i = 0; i < numberOfObjects; i++){
			Recycle();
		}
	}

	void Update () {
		if(objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled){
			Recycle();
		}
	}

	private void Recycle () {
		Vector3 scale = new Vector3(
			Random.Range(minSize.x, maxSize.x),
			Random.Range(minSize.y, maxSize.y),
			Random.Range(minSize.z, maxSize.z));

		Vector3 position = nextPosition;
		position.x += scale.x * 0.5f;
		position.y += scale.y * 0.5f;

		Transform o = objectQueue.Dequeue();
		o.localScale = scale;
		o.localPosition = position;
		objectQueue.Enqueue(o);

		nextPosition += new Vector3(
			Random.Range(minGap.x, maxGap.x) + scale.x,
			Random.Range(minGap.y, maxGap.y),
			Random.Range(minGap.z, maxGap.z));

		if(nextPosition.y < minY){
			nextPosition.y = minY + maxGap.y;
		}
		else if(nextPosition.y > maxY){
			nextPosition.y = maxY - maxGap.y;
		}
	}
}
이제 platforms 프리팹을 만들고, material 도 만들어보자.  skyline materials을 복사해서 칼라값만 (255, 60, 255)로 변경할수있다. Platform Regular Mat으로 변경하자. 새로운 cube를 만들고, materials을 붙이고 프리팹으로 만든다. 이름은 Platform으로 지정한다. 프리팹과 메트리얼은 Platform 폴더에 집어넣는다.
Platform prefab.
Platform Manager 이라는 빈 게임오브젝트를 만들고 Managers의 자식으로 놓아둔다.  Platform Manager 컴포넌트에 스크립트를 붙인다. 프리팹에 Platform prefab을 연결하고 Number Of Objects은 6으로 설정한다.  Recycle Offset 은 20으로 설정하고, Start Position 은 (0, 0, 0), Min Size 은 (5, 1, 1), Max Size 는 (10, 1, 1)으로 설정한다. 그리고 새로운 필드인 Min GapMax Gap,Min Y, 그리고 Max Y  는 각 각 (2, -1.5, 0), (4, 1.5, 0), -5, 그리고 10 으로 설정한다. 다시 말하지만 원한다면 니가 원하는 숫자로 설정할수있다.
platform manager managers
Platform manager.


Trackback 0 And Comment 0

[queue] Queue 관련 함수 (Dequeue, Enqueue, Peek)

|


Queue.Dequeue 메서드

Queue 의 시작 부분에서 개체를 제거하고 반환합니다.









Queue.Enqueue 메서드

개체를 Queue의 끝 부분에 추가합니다.





Queue.Peek 메서드
Queue 의 시작 부분에서 개체를 제거하지 않고 반환합니다.



Trackback 0 And Comment 0