![](/img/trans.png)
[英]I am making a 2d platformer game in unity engine, but I am unable to make my player jump
[英]My script is making the player to jump continuously and I want to make it to jump once or twice
我試圖為我的項目制作一個運動腳本,但我遇到了問題。 現在代碼在手機和電腦上運行良好,但只有一個小問題我無法真正解決它,基本上我被卡住了。 對於移動設備,如果您按下觸摸屏,播放器將使用速度並上升,直到您將手指從屏幕上移開。 我真正想要的是當手指觸摸手機的顯示屏而不是在天堂的 Y 軸上跳躍時最多跳躍一次或兩次。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehaviour : MonoBehaviour
{
/// <summary>
/// A reference to the rigidbody component
/// </summary>
private Rigidbody rb;
[Tooltip("How fast is goes side ways")]
public float dodgeSpeed;
public float jumpForce;
[Tooltip("How fast is goes forward!")]
// [Range(0,20)]
public float rollSpeed;
float dirX;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
dirX = Input.acceleration.x * dodgeSpeed;
var horizontalSpeed = Input.GetAxis("Horizontal") * dodgeSpeed;
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector3(horizontalSpeed + dirX, jumpForce, rollSpeed);
}
else
{
rb.AddForce(horizontalSpeed + dirX, 0, rollSpeed);
}
if (Input.touchCount > 0)
{
rb.velocity = new Vector3(horizontalSpeed + dirX, jumpForce, rollSpeed);
}
else
{
rb.AddForce(horizontalSpeed + dirX, 0, rollSpeed);
}
}
}
您可能想使用TouchPhase 。 在更新中,您可以檢查階段,並且只有在它剛剛開始時才跳轉:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
rb.velocity = new Vector3(horizontalSpeed + dirX, jumpForce, rollSpeed);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.