gpt4 book ai didi

c# - 怎么做rb.AddForce而不是2D? Unity C#

转载 作者:行者123 更新时间:2023-12-02 11:07:57 25 4
gpt4 key购买 nike

我正在制作一个2D游戏,您需要控制盾牌,并且必须阻止掉落的障碍物到达基地。我知道要移动某些内容,您可以使用rb.addforce。但是,当我对2D执行相同操作时,它将无法正常工作。我想象不是使用Vector3,而是使用Vector2,但是每次我编写该行时,它都会给我带来两个错误:Assets\Movement.cs(16,25): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2'和这个错误:Assets\Movement.cs(16,25): error CS1503: Argument 1: cannot convert from 'float' to 'UnityEngine.Vector2'。这是我的完整代码:

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

public class Movement : MonoBehaviour
{
public Rigidbody2D rb;
public Collider2D collider;

public float moveSpeed = 0.1f;

private void Update()
{
if (Input.GetKey("w"))
{
rb.AddForce(0f, moveSpeed);
Debug.Log("w");
}

if (Input.GetKey("s"))
{
rb.AddForce(0f, moveSpeed);
Debug.Log("s");
}
}
}

最佳答案

您可以在Vector2中添加新的rb.AddForce (new Vector2(0,moveSpeed));这是更新的代码:

 public class Movement : MonoBehaviour
{
public Rigidbody2D rb;
public Collider2D collider;

public float moveSpeed = 0.1f;

private void Update()
{
if (Input.GetKey("w"))
{
rb.AddForce(new Vector2(0f, moveSpeed));
Debug.Log("w");
}

if (Input.GetKey("s"))
{
rb.AddForce(new Vector2 (0f, moveSpeed));
Debug.Log("s");
}
}
}

关于c# - 怎么做rb.AddForce而不是2D? Unity C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62547761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com