首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法从System.Collections.Generic.List<UnityEngine.Vector3>转换为UnityEngine.Vector3

无法从System.Collections.Generic.List<UnityEngine.Vector3>转换为UnityEngine.Vector3
EN

Stack Overflow用户
提问于 2020-07-11 10:34:37
回答 1查看 2K关注 0票数 1

我正在制作一个全面的战争演示游戏,你拖动屏幕移动你的单位。这个bug一直困扰着我,我不知道背后的原因是什么。我得到的错误在下面。

'System.Collections.Generic.List‘(119,44):错误CS1503:参数1:无法从“UnityEngine.Vector3”转换为“UnityEngine.Vector3”

这是密码。(很抱歉它的长度)

代码语言:javascript
运行
复制
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Debug = UnityEngine.Debug;

public class FormationScript : MonoBehaviour
{
    public GameObject[] units;
    public Transform formationBarrier;

    float unitWidth = 2.0f; // This also includes the gap between them, in this case the unit width is 1 and the gap is also 1

    private float numberOfUnits;
    private double unitsPerRow;
    private float numberOfRows;

    Vector3 startClick;
    Vector3 endClick;

    Vector3 selectedAreaSize;
    Vector3 selectedAreaPos;

    float startMinusEndX;
    float startMinusEndZ;

    private List<List<Vector3>> availablePositions;

    // Start is called before the first frame update
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {
        Formation();
    }

    void Formation()
    {
        if (Input.GetMouseButtonDown(1))
        {
            Plane plane = new Plane(Vector3.up, 0);

            float distance;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (plane.Raycast(ray, out distance))
            {
                startClick = ray.GetPoint(distance);
            }
            Debug.Log(startClick);
        }
        if (Input.GetMouseButtonUp(1))
        {
            Plane plane = new Plane(Vector3.up, 0);

            float distance;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (plane.Raycast(ray, out distance))
            {
                endClick = ray.GetPoint(distance);
            }
            Debug.Log(endClick);
        }
        // ====================================================================================================== 

        /*if (startClick.x - endClick.x < 0 || startClick.z - endClick.z < 0)
        {
            startMinusEndX = startClick.x + endClick.x;
            startMinusEndZ = startClick.z + endClick.z;
        }
        else
        {
            startMinusEndX = startClick.x - endClick.x;
            startMinusEndZ = startClick.z - endClick.z;
        }
        if (startMinusEndX > 0 && startMinusEndZ > 0)
        {
            formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
            formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);
        }
        else if (startMinusEndX < 0 && startMinusEndZ > 0)
        {
            formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
            formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) * 2, 0, (startClick.z + endClick.z) / 2);
        }
        */
        startMinusEndX = startClick.x - endClick.x;
        startMinusEndZ = startClick.z - endClick.z;

        formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
        formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);

        // ====================================================================================================== 

        selectedAreaSize = formationBarrier.localScale;
        selectedAreaPos = formationBarrier.localPosition;

        numberOfUnits = units.Length;
        unitsPerRow = (unitWidth / numberOfUnits) * selectedAreaSize.x;

        unitsPerRow = Math.Round(unitsPerRow, 0);

        if (unitsPerRow < 0)
        {
            unitsPerRow = unitsPerRow * -2;
        }

        if (numberOfRows % 1 == 0)
        {
            numberOfRows = numberOfUnits % (float)unitsPerRow;
            for (int i = 0; i > numberOfRows; i++) // i is the number of rows
            {
                //availablePositions.Add(i);
                for (int j = 0; j > numberOfUnits / unitsPerRow; j++) // j is the number of units / the units per row
                {
                    Vector3 pos = new Vector3((selectedAreaPos.x / ((float)unitsPerRow + 1)) + ((j - 1) * (selectedAreaPos.x / ((float)unitsPerRow + 1))), 0.0f, i * 2);
                    availablePositions.Add(pos); // Heres where the error's coming from
                }
            }
        }
        else if (numberOfUnits % (float)unitsPerRow != 0)
        {
            numberOfRows = numberOfUnits % (float)unitsPerRow;
        }

        Debug.Log(unitsPerRow);
        Debug.Log(numberOfRows);
    }
}

我对团结很陌生,所以别着急:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-11 11:04:56

代码中有一个语法错误。您正在向availablePositions,中插入pos (类型为Vector3 ),这是Vecotr3列表的列表。

要么更改availablePositions定义:

代码语言:javascript
运行
复制
private List<Vector3> availablePositions;

或在添加到availablePositions之前将pos转换为list:

代码语言:javascript
运行
复制
availablePositions.Add(new List<Vector3>{pos});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62848034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档