简体   繁体   English

为什么错误显示“ArgumentException:索引越界”。 当我在 Unity 中获得触摸增量位置时?

[英]Why error is showing "ArgumentException: Index out of bounds." when I get touch delta position in Unity?

I am trying to rotate a gameObject using raycast.我正在尝试使用 raycast 旋转游戏对象。 When I run unity editor getting the error当我运行统一编辑器时出现错误

ArgumentException: Index out of bounds. ArgumentException:索引越界。 UnityEngine.Input.GetTouch (Int32 index) (at /Users/builduser/buildslave/unity/build/artifacts/generated/bindings_old/common/Core/InputBindings.gen.cs:619) AdjustTransform.Update () (at Assets/AdjustTransform.cs:27) UnityEngine.Input.GetTouch (Int32 index) (at /Users/builduser/buildslave/unity/build/artifacts/generated/bindings_old/common/Core/InputBindings.gen.cs:619) AdjustTransform.Update () (at Assets/AdjustTransform .cs:27)

Line 27 is Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;第 27 行是Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition; in the below code.在下面的代码中。 What I am doing wrong here?我在这里做错了什么?

 void Update()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

    if (Physics.Raycast(ray,out hit,100))
    {

        Debug.Log(" GO Name "+hit.collider.gameObject.name);
    }

if(  Input.touchCount == 2 && !EventSystem.current.IsPointerOverGameObject() )
    {


            hit.collider.gameObject.transform.Rotate(Vector3.up, -touchDeltaPosition.x * rotspeed * Time.deltaTime, Space.World);
            hit.collider.gameObject.transform.Rotate(Vector3.right, touchDeltaPosition.y * rotspeed * Time.deltaTime, Space.World);


    }

Input.GetTouch uses an index to find the status of a specific touch.Input.GetTouch使用索引来查找特定触摸的状态。 If there are no touches then it throws the Index out of bounds error.如果没有触摸,则会抛出Index out of bounds错误。

Since you are calling the code in your Update method, it is being checked every frame, even if you haven't had any input to your game.由于您在Update方法中调用代码,因此每帧都会对其进行检查,即使您没有对游戏进行任何输入。

What you need to do is check that there are touches since the last time Update was called using Input.touchCount , then get the touch:您需要做的是检查自上次使用Input.touchCount调用Update以来是否有触摸,然后获取触摸:

if (Input.touchCount > 0)
{
    Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
}

One of the problems that I've realized with this is that every time that I put the line Input.GetTouch(0) outside of the if-statement like below I get an error:我已经意识到的问题之一是,每次将Input.GetTouch(0)Input.GetTouch(0) if 语句之外时,如下所示,我都会收到错误消息:

Input.GetTouch(0);

if (Input.touchCount > 0)
{

}

However, if I keep it inside the if-statement like below the error is gone:但是,如果我将其保留如下所示的 if 语句中,则错误消失了:

if (Input.touchCount > 0)
{
    Input.GetTouch(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 复制字节时出错 - 偏移量和长度超出范围。 - Error when copying bytes - offset and length were out of bounds. 在 Unity 中渲染网格时出现索引越界错误 - Index out of bounds error when rendering mesh in Unity 调整数组大小时,为什么索引超出数组范围? - Why is the index out of bounds of the array when I'm resizing the array? 为什么我从数据库中获取索引超出范围错误 - Why am I getting index out of bounds error from database 我可以在 Unity 中同时触摸 position 和鼠标 position 吗? - Can I get touch position and mouse position at once in Unity? 不同屏幕尺寸的Unity touch增量位置不正确 - Unity touch delta position incorrect with different screen sizes 为什么我无法获得统一触摸输入? - Why I cant get unity touch input? 如果未设置索引,则datagridview索引超出范围错误 - datagridview index out of bounds error in event when no index is being set 为什么拖动时gameobject / raycast没有准确地跟随触摸position统一 - why gameobject / raycast when dragged is not accurately following the touch position in unity 使用统一引擎时,我收到错误 ArgumentException: Input Button w is not setup。 要更改输入设置,请使用:编辑 -> 设置 -> 输入 - When using unity engine I get the error ArgumentException: Input Button w is not setup. To change the input settings use: Edit -> Settings -> Input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM