[unity]2 Dフラットロケータ-プレイヤージャンプを実現
ジャンプ
ジャンプの実現は簡単だ.
次のコードを追加します.
しかも下がる速度が遅く、重力を変えることができます.
1.プロジェクトの設定->Gravity Y値の変更方法
2.Rigidbody Scale>オブジェクトに適した重力スケールを変更できます.
アニメーション(Animation)
ジャンプするときは歩く動画は外したほうがいい!!
だから私はあなたに新しいアニメーションを作ってあげました.
このように接続し、コードを追加します.
しかし問題が発生した.
つまり、ジャンプが終わってもジャンプから->idleにジャンプしないということです.
コードをさらに修正する必要があります
「レケスト」を勉強します.
(オブジェクトを検索するためにRayを送信します.
)
レーザーをプラットフォームに落とすたびに
関数を実行して次のアニメーションに移動します.
またジャンプも無限ジャンプを防ぐために作られたコードは以下の通りです.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public float maxSpeed;// 최대속도 설정
public float jumpPower; // 점프파워
Rigidbody2D rigid;
SpriteRenderer spriteRenderer;
Animator anim;
void Awake()
{
rigid = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
void Update()
{
//Jump
if (Input.GetButtonDown("Jump")&& !anim.GetBool("isJumping"))
{
rigid.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse); // 이르케 점프한다 !
anim.SetBool("isJumping", true);
}
// Stop Speed
if (Input.GetButtonUp("Horizontal"))
{
rigid.velocity = new Vector2(rigid.velocity.normalized.x * 0.5f, rigid.velocity.y);//float곱할때는 f붙여줘야한다.
}
// change Direction
if(Input.GetButtonDown("Horizontal"))
spriteRenderer.flipX = Input.GetAxisRaw("Horizontal") == -1;
// work animation
if (Mathf.Abs( rigid.velocity.x)< 0.3) //절댓값 설정
anim.SetBool("isWorking", false);
else
anim.SetBool("isWorking", true);
}
void FixedUpdate()
{
// Move by Control
float h = Input.GetAxisRaw("Horizontal"); // 횡으로 키를 누르면
rigid.AddForce(Vector2.right*h,ForceMode2D.Impulse); // 이르케 이동한다 !
// MaxSpeed Limit
if (rigid.velocity.x > maxSpeed)// right
rigid.velocity = new Vector2(maxSpeed, rigid.velocity.y);
else if (rigid.velocity.x < maxSpeed * (-1)) // Left Maxspeed
rigid.velocity = new Vector2(maxSpeed * (-1), rigid.velocity.y);
// Lending Platform
if(rigid.velocity.y < 0)
{
//Debug.DrawRay(rigid.position, Vector3.down, new Color(0, 1, 0)); //에디터 상에서만 레이를 그려준다
RaycastHit2D rayHit = Physics2D.Raycast(rigid.position, Vector3.down, 1, LayerMask.GetMask("Platform"));
if (rayHit.collider != null) // 바닥 감지를 위해서 레이저를 쏜다!
{
if (rayHit.distance < 0.5f)
{
anim.SetBool("isJumping", false);
}
}
}
}
}
ジャンプまで実施しました!!Reference
この問題について([unity]2 Dフラットロケータ-プレイヤージャンプを実現), 我々は、より多くの情報をここで見つけました https://velog.io/@seochan99/unity2D-플랫포머-플레이어-점프-구현하기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol