【问题标题】:I am having problem with this error: cannot convert from 'UnityEngine.ForceMode2D' to 'UnityEngine.ForceMode'. Please Help Me solve it [closed]我遇到了这个错误的问题:无法从“UnityEngine.ForceMode2D”转换为“UnityEngine.ForceMode”。请帮我解决它[关闭]
【发布时间】: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


【解决方案1】:

您正在混合 2D 和 3D 功能。

二维

private Rigidbody2D mybody;

...

mybody.AddForce(new Vector2(0f, jumpforce), ForceMode2D.Impulse);

3D

private Rigidbody mybody;

...

mybody.AddForce(new Vector2(0f, jumpforce, 0f), ForceMode.Impulse);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2021-09-05
    • 2021-05-26
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多