我需要帮助来解决Unity 3D中CS1503错误“无法从'字符串转换为'浮动'的问题”

时间:2019-10-25 00:41:15

标签: c# visual-studio unity3d

我正在尝试创建一个脚本,该脚本将在Unity3D中移动我的对象,并且遇到错误。

我已经搜寻了代码的问题,但找不到解决方案,大多数解决方案都涉及在尝试替换向量之前添加新标签,但是我已经做到了。

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

public class Player : MonoBehaviour
{
    public float moveSpeed = 10.0f;
    public Rigidbody2D player;
    // Start is called before the first frame update
    void Start()
    {
        player = this.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        MovePlayer();
    }
    public void MovePlayer()
    {
     // This is where the error is being thrown
        player.velocity = new Vector2 (Input.GetAxis ("Horizontal"), ("Vertical")) * moveSpeed;
    // This is where the error is being thrown
    }
}

我希望这可以使我使用a和d键控制对象,以便左右移动对象,但是会引发错误CS1503参数2:无法从字符串转换为浮点型

1 个答案:

答案 0 :(得分:1)

我未能在“垂直”之前添加Input.GetAxis,该行应如下所示

player.velocity = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")) * moveSpeed;