Commit f970dda9 authored by abpo11's avatar abpo11

플레이어가 로프를 탈 수 있게됨. 로프에 닿은 상태에서 점프시 타고, 좌우 방향키만 누를시 내림

parent c58083ac
...@@ -393,10 +393,15 @@ MonoBehaviour: ...@@ -393,10 +393,15 @@ MonoBehaviour:
speed: 600 speed: 600
speedAir: 5 speedAir: 5
jumpSpeed: 400 jumpSpeed: 400
ropeSpeed: 200
doubleJumpSpeed: 400 doubleJumpSpeed: 400
groundLayer: groundLayer:
serializedVersion: 2 serializedVersion: 2
m_Bits: 256 m_Bits: 256
ropeLayer:
serializedVersion: 2
m_Bits: 512
ropeDistance: 0.3
rayDistance: 1 rayDistance: 1
--- !u!212 &489222436 --- !u!212 &489222436
SpriteRenderer: SpriteRenderer:
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class PlayerController : MonoBehaviour { public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb; // RigidBody2D of this game object private Rigidbody2D rb; // RigidBody2D of this game object
// Speeds of player // Speeds of player
...@@ -15,34 +16,42 @@ public class PlayerController : MonoBehaviour { ...@@ -15,34 +16,42 @@ public class PlayerController : MonoBehaviour {
[SerializeField] [SerializeField]
private float jumpSpeed; private float jumpSpeed;
[SerializeField] [SerializeField]
private float ropeSpeed;
[SerializeField]
private float doubleJumpSpeed; private float doubleJumpSpeed;
// Bool values for jump & doublejump // Bool values for jump & doublejump
private bool isGrounded = true; private bool isGrounded = true;
private bool isJumpable = true; // Can player jump or doublejump? private bool isJumpable = true; // Can player jump or doublejump?
private bool isInRope = false;
// Inputs // Inputs
private float horizontal = 0; private float horizontal = 0;
private float horizontalRaw = 0; private float horizontalRaw = 0;
private float verticalRaw = 0;
private bool jump = false; private bool jump = false;
// Variables for IsGrounded() // Variables for IsGrounded()
[SerializeField] [SerializeField]
private LayerMask groundLayer; private LayerMask groundLayer;
[SerializeField] [SerializeField]
private LayerMask ropeLayer;
[SerializeField]
private float ropeDistance = 0.3f;
[SerializeField]
private float rayDistance; private float rayDistance;
// Use this for initialization // Use this for initialization
void Start () { void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>(); rb = gameObject.GetComponent<Rigidbody2D>();
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update()
{
horizontal = Input.GetAxis("Horizontal"); horizontal = Input.GetAxis("Horizontal");
horizontalRaw = Input.GetAxisRaw("Horizontal"); horizontalRaw = Input.GetAxisRaw("Horizontal");
verticalRaw = Input.GetAxisRaw("Vertical");
if (Input.GetButtonDown("Jump")) if (Input.GetButtonDown("Jump"))
{ {
jump = true; jump = true;
...@@ -55,32 +64,59 @@ public class PlayerController : MonoBehaviour { ...@@ -55,32 +64,59 @@ public class PlayerController : MonoBehaviour {
if (isGrounded) if (isGrounded)
isJumpable = true; isJumpable = true;
if (IsInRope())
{
if (isInRope)
{
if (horizontalRaw != 0f && verticalRaw == 0f)
{
isInRope = false;
rb.gravityScale = 2f;
}
rb.velocity = new Vector2(0f, verticalRaw * Time.smoothDeltaTime * ropeSpeed);
}
else if (jump)
{
isInRope = true;
rb.gravityScale = 0f;
transform.position = new Vector2(Mathf.Round(transform.position.x - 0.5f) + 0.5f, transform.position.y);
rb.velocity = new Vector2(0f, 0f);
}
}
else
{
isInRope = false;
rb.gravityScale = 2f;
}
if (!isInRope)
{
float vertical = rb.velocity.y; float vertical = rb.velocity.y;
if (jump) if (jump)
{ {
if (isGrounded) if (isGrounded)
{ {
vertical = jumpSpeed * Time.fixedDeltaTime; vertical = jumpSpeed * Time.smoothDeltaTime;
} }
else if (isJumpable) else if (isJumpable)
{ {
vertical = doubleJumpSpeed * Time.fixedDeltaTime; vertical = doubleJumpSpeed * Time.smoothDeltaTime;
isJumpable = false; isJumpable = false;
} }
} }
//rb.velocity = new Vector2(horizontal * speed * Time.fixedDeltaTime, vertical); //rb.velocity = new Vector2(horizontal * speed * Time.smoothDeltaTime, vertical);
// rb.velocity = new Vector2(rb.velocity.x, vertical); // rb.velocity = new Vector2(rb.velocity.x, vertical);
rb.AddForce(horizontalRaw * speed * Time.fixedDeltaTime * Vector2.right); rb.AddForce(horizontalRaw * speed * Time.smoothDeltaTime * Vector2.right);
if (((horizontalRaw==0) || (rb.velocity.x > 0 && horizontalRaw < 0) if (((horizontalRaw == 0) || (rb.velocity.x > 0 && horizontalRaw < 0)
|| (rb.velocity.x < 0 && horizontalRaw > 0)) &&(isGrounded)) || (rb.velocity.x < 0 && horizontalRaw > 0)) && (isGrounded))
{ {
rb.AddForce(rb.velocity.x * (-10f) * Vector2.right); rb.AddForce(rb.velocity.x * (-10f) * Vector2.right);
} }
rb.velocity = new Vector2(Mathf.Clamp(rb.velocity.x, -maxSpeed*Time.fixedDeltaTime, maxSpeed * Time.fixedDeltaTime), vertical); rb.velocity = new Vector2(Mathf.Clamp(rb.velocity.x, -maxSpeed * Time.smoothDeltaTime, maxSpeed * Time.smoothDeltaTime), vertical);
}
jump = false; jump = false;
} }
bool IsGrounded() // Is player grounded? bool IsGrounded() // Is player grounded?
...@@ -89,4 +125,12 @@ public class PlayerController : MonoBehaviour { ...@@ -89,4 +125,12 @@ public class PlayerController : MonoBehaviour {
Debug.DrawRay(transform.position, rayDistance * Vector2.down, Color.white); Debug.DrawRay(transform.position, rayDistance * Vector2.down, Color.white);
return hit.collider != null; return hit.collider != null;
} }
bool IsInRope() // Is player in rope?
{
RaycastHit2D hit1 = Physics2D.Raycast(transform.position, Vector2.right, ropeDistance, ropeLayer);
RaycastHit2D hit2 = Physics2D.Raycast(transform.position, Vector2.left, ropeDistance, ropeLayer);
Debug.DrawRay(transform.position, ropeDistance * Vector2.right, Color.red);
Debug.DrawRay(transform.position, ropeDistance * Vector2.left, Color.red);
return hit1.collider != null || hit2.collider != null;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment