【发布时间】:2022-11-01 21:30:46
【问题描述】:
当我按下 Space 按钮时,我正试图在 Unity 2D 中进行方形跳跃。我有以下代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float moveSpeed;
public float jumpSpeed;
float moveX;
Vector2 pos;
// Update is called once per frame
void Update()
{
MoveHorizontally();
Jump();
}
void MoveHorizontally(){
moveX = Input.GetAxis("Horizontal") * Time.deltaTime;
pos.x = moveX * moveSpeed;
transform.position = new Vector2(transform.position.x + pos.x,transform.position.y + pos.y);
}
void Jump(){
if (Input.GetKeyDown("space")){
GetComponent<Rigidbody2D>().AddForce(new Vector2(0,jumpSpeed * Time.deltaTime), ForceMode2D.Impulse);
Debug.Log("Jumped!");
}
}
}
当我按下空格键时,“跳了!”消息出现了,但我的方块没有跳跃。任何想法?
非常感谢。
我尝试使用 Input.GetKey 函数。它可以工作,但是如果我一直按住空格键,这个功能会让我的方块向上移动。
【问题讨论】:
-
你的力量会很小。
-
When I press Space button, "Jumped!" message shows up.. 听起来你的问题标题不是真的...... -
你给 jumpForce 一个低值,将它设置为 500 或更多,你会看到你的跳跃。
标签: unity3d