【问题标题】:How can I fix the following error, 'RenderBuffer' does not contain a definition for 'velocity', in unity c#?如何修复以下错误,\'RenderBuffer\' 不包含 unity c# 中的 \'velocity\' 定义?
【发布时间】:2023-01-29 20:36:14
【问题描述】:

我有一个简单的播放器移动脚本,但我遇到了以下错误“RenderBuffer”不包含“速度”的定义,我不知道如何修复它。

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting.Antlr3.Runtime.Tree;
using UnityEngine;

public class PlayerMovement : MonoBehaviour

{
    private Rigidbody2D rb;
    private BoxCollider2D coll;

    private bool hasDoubleJumped = false;

    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpForce = 14f;

    [SerializeField] private LayerMask jumpableGround;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
    }

    private void Update()
    {
        float dirX = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            hasDoubleJumped = false;
            rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
        }

        else if (Input.GetButtonDown("Jump") && !hasDoubleJumped)
        {
            hasDoubleJumped = true;
            rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
        }
    }

    private bool IsGrounded()
    {
        return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    }
}

我试过改变一些东西,但它似乎并没有解决我的问题。

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    看起来问题出在代码 RenderBuffer.velocity 上,它试图访问 RenderBuffer 的速度属性,但速度不是 RenderBuffer 的属性。我注意到您也有类似 rb.veclocity 的代码,其中 rb 指的是 RigidBody(它确实具有速度属性)。你能在这里打错字吗?也许您只需要在代码中将 RigidBody.velocity 替换为 rb.velocity 即可。

    【讨论】:

      【解决方案2】:

      在你的跳跃逻辑上你写了RenderBuffer.velocity.x,似乎不合适,如果你想访问当前的刚体速度,试试rb.velocity.x

      【讨论】:

        猜你喜欢
        • 2021-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多