unity3d 如何修复无法从“float”转换为“UnityEngine.Vector2”

myzjeezk  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(510)

在vs代码中,我得到了这个错误“参数2:无法从“浮点型”转换为“UnityEngine.Vector2”[程序集-CSharp]csharp(CS1503)"。
以下是出现错误的代码:

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

public class HF_001 : MonoBehaviour
{
    
    public enum ForceType { Repulsion = -1, None = 0, Attraction = 1 }
    public ForceType m_Type;
    public Transform m_Pivot;
    public float m_Radius;
    public float m_StopRadius;
    public float m_Force;
    public LayerMask m_Layers;

    
    
    private void FixedUpdate()
    {
        Collider2D[] colliders = Physics2D.OverlapArea(m_Pivot.position, m_Radius, m_Layers);

        float signal = (float)m_Type;

        foreach (var collider in colliders)
        {
            Rigidbody2D body = collider.GetComponent<Rigidbody2D>();
            if (body == null) 
                continue;

            Vector2 direction = m_Pivot.position - body.position;

            float distance = direction.magnitude;

            direction = direction.normalized;

            if (distance < m_StopRadius) 
                continue;

            float forceRate = (m_Force / distance);

            body.AddForce(direction * (forceRate / body.mass) * signal);
        }
    }
}

我尝试更改变量“m_Radius”的类型

zsbz8rwp

zsbz8rwp1#

根据Physics2D.OverlapArea的官方文档。它采用第二个参数Vector2,而不是浮点值。
在上面的代码中,public float m_Radius;是浮点类型,但必须是Vector2
Physics2D.OverlapArea用于检查碰撞器是否福尔斯在一个矩形区域内,如果您需要使用半径而不是点,请查看Physics2D.OverlapCircle

相关问题