unityのジャンプの挙動が変なので直したいです。今はスペースキーを少しだけ押すと上にわずかに上がるのですが、押したら押した時間に関係なく一定量上がるようにしたいです。
あとスマホ用に画面のボタンを押すとジャンプする関数を作りたいのですが、 色々試しましたがうまく実装できませんでした。
isGroundedの接地判定が平面でもfalseになるので、キャラクターの下にPhysics.RayCastを飛ばして判定を取得しています。
よろしくお願いします。
void Start()
{
if (myPV.IsMine) //自キャラであれば実行
{
//MainCameraのtargetにこのゲームオブジェクトを設定
mainCam = Camera.main;
mainCam.GetComponent<CCameraScript>().target = this.gameObject.transform;
}
}
// Update関数は1フレームに1回実行される
void Update()
{
if (!myPV.IsMine)
{
return;
}
moveControl(); //移動用関数
RotationControl(); //旋回用関数
//最終的な移動処理
//(これが無いとCharacterControllerに情報が送られないため、動けない)
controller.Move(moveDirection * Time.deltaTime);
}
void moveControl()
{
//★進行方向計算
//キーボード入力を取得
float v = Input.GetAxisRaw("Vertical"); //InputManagerの↑↓の入力
float h = Input.GetAxisRaw("Horizontal"); //InputManagerの←→の入力
Debug.Log("v==============" + v);
h = PlayerManager.playerx;
v = PlayerManager.playery;
//カメラの正面方向ベクトルからY成分を除き、正規化してキャラが走る方向を取得
Vector3 forward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
Vector3 right = Camera.main.transform.right; //カメラの右方向を取得
//カメラの方向を考慮したキャラの進行方向を計算
targetDirection = h * right + v * forward;
//真下にレイを飛ばして接地判定
rayPosition = transform.position + new Vector3(0.0f, 0.1f, 0.0f);
ray = new Ray(rayPosition, Vector3.down);
isGrounded = Physics.Raycast(ray, rayDistance);
//Debug.Log("isGrounded==============" + isGrounded);
//★地上にいる場合の処理
if (isGrounded)
{
//移動のベクトルを計算
moveDirection = targetDirection * speed;
//Jumpボタンでジャンプ処理
if (Input.GetButton("Jump"))
{
//animator.SetBool("is_jumping", true);
Debug.Log("ジャンプ処理");
moveDirection.y = jumpSpeed;
}
}
else //空中操作の処理(重力加速度等)
{
//animator.SetBool("is_jumping", false);
float tempy = moveDirection.y;
//(↓の2文の処理があると空中でも入力方向に動けるようになる)
//moveDirection = Vector3.Scale(targetDirection, new Vector3(1, 0, 1)).normalized;
//moveDirection *= speed;
moveDirection.y = tempy - gravity * Time.deltaTime;
}
//★走行アニメーション管理 if (v > .1 || v < -.1 || h > .1 || h < -.1) //(移動入力があると) { animator.SetBool("is_running", true); //animator.SetFloat("Speed", 1f); //キャラ走行のアニメーションON } else //(移動入力が無いと) { animator.SetBool("is_running", false); // animator.SetFloat("Speed", 0f); //キャラ走行のアニメーションOFF } } void RotationControl() //キャラクターが移動方向を変えるときの処理 { Vector3 rotateDirection = moveDirection; rotateDirection.y = 0; //それなりに移動方向が変化する場合のみ移動方向を変える if (rotateDirection.sqrMagnitude > 0.01) { //緩やかに移動方向を変える float step = rotateSpeed * Time.deltaTime; Vector3 newDir = Vector3.Slerp(transform.forward, rotateDirection, step); transform.rotation = Quaternion.LookRotation(newDir); } } public void Onjump() {
//ここにジャンプ処理を追加したいです
}
}
あなたの回答
tips
プレビュー