【发布时间】:2022-01-11 23:43:45
【问题描述】:
目前我正在通过 Unity3d 开发一款游戏。我必须在地上冲动地跳跃我的角色。我正在使用从 Youtube 学习的命令。在老师的游戏中一切似乎都很好,但我的却不工作。请帮助我为什么它不起作用?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField]
private float Moveforce = 10f;
[SerializeField]
private float jumpforce = 11f;
private float Movementx;
private Rigidbody mybody;
private SpriteRenderer sr;
private Animator anim;
private string WALK_ANIMATION = "Walk";
private void Awake()
{
mybody = GetComponent<Rigidbody>();
sr = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
void Start()
{
}
// Update is called once per frame
void Update()
{
PlayerMoveKeyboard();
AnimatePlayer();
}
private void FixedUpdate()
{
PlayerJump();
}
void PlayerJump()
{
if (Input.GetButtonDown("Jump"))
{
mybody.AddForce(new Vector2(0f, jumpforce), ForceMode2D.Impulse);
}
}
【问题讨论】:
-
您使用的是 3d 刚体。不是二维的。哪个是正确的?
-
好的,我遇到了问题,我错误地使用了 Rigidbody 组件而不是 RgiBody2D。
标签: c# unity3d game-development