Unity3Dでマリオのジャンプ
Unity 2020.2.7
参考元
Unityの場合
多分動くコード
適当な3Dモデルのゲームオブジェクトにアタッチ
Use GravityはOFFで
using System.Collections; using System.Collections.Generic; using System; using UnityEngine; public class MyMarioJump : MonoBehaviour { [SerializeField] float jumpPower = 3f; // ジャンプ力 [SerializeField] float fallPower = -5f; // 落下力 private GroundCheck gr; // CharacterControllerのisGroundedでも可 bool jumping = false; // ジャンプ中かどうかのフラグ float yPrev; // 前フレームのtransform.potision.yの値 float f; // 最初のフレームはジャンプ力、それ以降は落下力が入力される void Jump() { Vector3 tmp = transform.position; Vector3 t = transform.position; bool isFalling = transform.position.y < yPrev; t.y = (transform.position.y - yPrev) + f; t.x = 0f; t.z = 0f; transform.position += t; yPrev = tmp.y; f = GetFallPower(); if (gr.isGrounded && isFalling) { Debug.Log("ground"); jumping = false; } } // SerializeFieldで使いやすいようにここで値を小さくする private float GetJumpPower() { return jumpPower / 100; } private float GetFallPower() { return fallPower / 10000; } private void Start() { gr = GetComponent<GroundCheck>(); f = GetFallPower(); } void Update() { if (jumping) { Jump(); return; } if (Input.GetKeyDown(KeyCode.X)) { if (!jumping) { Debug.Log("jump"); jumping = true; f = GetJumpPower(); yPrev = transform.position.y; } } } }