unity3d 错误CS1503:论据二:无法从“UnityEngine.Vector2”转换为“浮点型”

pbpqsu0x  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(468)

我正在为我的侧滚游戏的运动脚本教程,但我遇到了这个错误"error CS1503:论据二:无法从"UnityEngine.Vector2"转换为"浮点型""
这是代码,我想问题出在FixedUpdate中,但我不确定

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Movement : MonoBehaviour
{
    private float horizontal;
    private float speed = 8f;
    private bool isFacingRight = true;

    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundLayer;

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");

        Flip();
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(horizontal * speed, rb.velocity);
    }

    private void Flip()
    {
        if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
        {
            isFacingRight = !isFacingRight;
            Vector3 localScale = transform.localScale;
            localScale.x *= -1f;
            transform.localScale = localScale;
        }
    }
}
nnvyjq4y

nnvyjq4y1#

在FixedUpdate中,您尝试将vector 2 y分量设置为Vector 2,而它应该是浮点型。

private void FixedUpdate()
{
    rb.velocity = new Vector2(horizontal * speed, rb.velocity);
}

你的意思很可能是:

private void FixedUpdate()
{
    rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}

这里你把速度的y部分设置为旧速度的y部分的两倍,保持不变,这里rb.velocity.y是一个浮点数,新的Vector 2需要两个浮点数,希望这能有所帮助!

相关问题