Preview
필요한 Script
- Player
- Ground Sensor
- Moving Platform
Player
필요한 Component;
- Sprite Renderer
- Box Collider 2D : Friction & Bounciness가 모두 0인 Physical Material 2D가 필요하다. 플레이어가 벽에 달라붙지 않게 하기 위함이다.
- Rigidbody 2D : Body Type(Dynamic), Gravity Scale(6), Collision Detection(Continuous), Constrains(Freeze Rotation Z만 체크)
- Player(Script) : 하단 참조.
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D rb;
private float speed;
private float vel_damp;
private float jump_force;
private Vector2 acc_direction;
private bool is_grounded; public void SetIsGrounded(bool value) { is_grounded = value; }
private void Awake()
{
GetReferences();
InitFields();
}
private void GetReferences()
{
rb = GetComponent<Rigidbody2D>();
}
private void InitFields()
{
speed = 125f;
vel_damp = 0.75f;
jump_force = 17.5f;
acc_direction = Vector2.zero;
is_grounded = false;
}
private void FixedUpdate()
{
AdjustMovement();
}
private void AdjustMovement()
{
rb.linearVelocity += acc_direction * speed * Time.fixedDeltaTime;
rb.linearVelocity = new Vector2(rb.linearVelocity.x * vel_damp, rb.linearVelocity.y);
}
private void Update()
{
Move();
Jump();
}
private void Move()
{
acc_direction = Vector2.zero;
if (Input.GetKey(KeyCode.RightArrow)) acc_direction += Vector2.right;
if (Input.GetKey(KeyCode.LeftArrow)) acc_direction += Vector2.left;
}
private void Jump()
{
if (Input.GetKeyDown(KeyCode.UpArrow) && is_grounded)
{
rb.linearVelocity = new Vector2(rb.linearVelocityX, jump_force);
}
}
}
Ground Sensor
(Player GameObject의 Child여야 한다)
필요한 Component;
- Box Collider 2D : Is Trigger(체크)
- GroundSensor(Script) : 하단 참조.
using UnityEngine;
public class GroundSensor : MonoBehaviour
{
private Player player;
private void Awake()
{
GetReferences();
//InitFields();
}
private void GetReferences()
{
player = GetComponentInParent<Player>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Ground")
{
player.SetIsGrounded(true);
}
}
// 매우 정확한 물리적-판정이 요구되는 게임에만 사용할 것.
/*private void OnTriggerStay2D(Collider2D other)
{
if (other.tag == "Ground")
{
player_script.SetIsGrounded(true);
return;
}
}*/
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Ground")
{
player.SetIsGrounded(false);
}
}
}
Moving Platform
(Tag : Ground)
필요한 Component;
- Sprite Renderer
- Box Collider 2D
- MovingPlatform(Script) : 하단 참조.
using UnityEngine;
public class MovingPlatform : MonoBehaviour
{
private Rigidbody2D rb;
private Vector2 destination;
// prefab화를 통해 사용하기 위함.
[SerializeField] private float speed;
[SerializeField] private Vector2 start_pos, end_pos;
private void Awake()
{
GetReferences();
InitFields();
}
private void GetReferences()
{
rb = GetComponent<Rigidbody2D>();
}
private void InitFields()
{
destination = end_pos;
}
private void Start()
{
transform.position = start_pos;
}
private void Update()
{
Move();
}
private void Move()
{
transform.Translate((destination - (Vector2)transform.position).normalized * speed * Time.deltaTime);
if (Vector2.Distance(transform.position, destination) <= 0.125f)
{
if (destination == start_pos) { destination = end_pos; }
else { destination = start_pos; }
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.GetComponent<GroundSensor>())
{
other.transform.parent.transform.SetParent(transform);
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.GetComponent<GroundSensor>())
{
other.transform.parent.transform.SetParent(null);
}
}
}