【问题标题】:touch control to move cube (in an array that generates them) left and right触摸控制左右移动立方体(在生成它们的数组中)
【发布时间】:2017-01-08 21:55:46
【问题描述】:

我正在移动一个由两个立方体组成的对象:左和右。这些立方体是在 y 轴上随机生成的。

我可以毫无问题地左右移动它们,但是,当我左右移动其中一个立方体时,它们都会移动。

我怎样才能在仅向左或向右触摸而不是全部移动时仅移动其中一个立方体?下面是我的代码:

生成立方体代码:

public Transform block;
public Transform player;
private float objectSpawnedTo = 5.0f;
public static float distanceBetweenObjects = 5.0f;
private float nextCheck = 0.0f;
private ArrayList objects = new ArrayList();
void Start () {
    maintenance(0.0f);
}


void Update () {
    float playerX = player.position.y;
    if(playerX > nextCheck)
    {
        maintenance(playerX);
    }
}

private void maintenance(float playerX)
{
    nextCheck = playerX + 30;
    for (int i = objects.Count-1; i >= 0; i--) 
    {
        Transform blck = (Transform)objects[i];
        if(blck.position.y < (transform.position.y - 30))
        {
            Destroy(blck.gameObject);
            objects.RemoveAt(i);
        }
    }
    spawnObjects(5);
}

private void spawnObjects(int howMany)
{
    float spawnX = objectSpawnedTo;
    for(int i = 0; i<howMany; i++)
    {
        Vector3 pos = new Vector3(-3.5f,spawnX, 0);
        //float firstRandom = Random.Range(-6.0f, 1.0f);
        Transform blck = (Transform)Instantiate(block, pos, Quaternion.identity);
        //blck.localScale+=new Vector3(firstRandom*2,0,0);
        objects.Add(blck);
        //pos = new Vector3(0,spawnX, 0);
        //blck = (Transform)Instantiate(block, pos, Quaternion.identity);
        //blck.localScale +=new Vector3((8.6f-firstRandom)*2,0,0);
        //objects.Add(blck);
        spawnX = spawnX + distanceBetweenObjects;
    }
    objectSpawnedTo = spawnX;
}

}

立方体控制代码:

public float speed = 5;

// Use this for initialization
void Start () {

}

void Update() {
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
        // Get movement of the finger since last frame
        Vector3 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

        // Move object across XY plane
        transform.Translate(touchDeltaPosition.x * speed, 0, 0);


        Vector3 boundaryVector = transform.position;   
        boundaryVector.x = Mathf.Clamp (boundaryVector.x, -5.5f, -2.8f);
        transform.position = boundaryVector;
    }
}

}

【问题讨论】:

    标签: c# ios unity3d


    【解决方案1】:

    问题是您的多维数据集控制代码中没有关于选择哪个多维数据集的条件:您只需等待Input.touchCount > 0,然后移动多维数据集。所以所有有这个脚本的立方体都会移动。

    我认为你需要做的是一个光线投射来检查哪个立方体被“触摸”,然后只有在光线投射成功时才移动它:

    [SerializeField]
    private float speed = 0.5f;
    
    private int MAX_TOUCH_COUNT = 5;
    private bool[] touched;
    
    protected void Start()
    {
        touched = new bool[MAX_TOUCH_COUNT];
    }
    
    void Update()
    {
        if (Input.touchCount > 0)
        {
            for(int i = 0; i < (Input.touchCount <= MAX_TOUCH_COUNT ? Input.touchCount : MAX_TOUCH_COUNT); i++)
            {
                if(touched[i] && Input.GetTouch(i).phase == TouchPhase.Ended)
                {
                    touched[i] = false;
                }
                else if (!touched[i] && Input.GetTouch(i).phase == TouchPhase.Began)
                {
                    Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
                    RaycastHit hitInfo;
                    if (Physics.Raycast(ray, out hitInfo))
                    {
                        if (hitInfo.transform.GetComponentInChildren<*YOUR_CUBE_CONTROL_CLASS_NAME*>() == this)
                        {
                            touched[i] = true;
                        }
                    }
                }
                else if (touched[i] && Input.GetTouch(i).phase == TouchPhase.Moved)
                {
                    // Get movement of the finger since last frame
                    Vector3 touchDeltaPosition = Input.GetTouch(i).deltaPosition;
    
                    // Move object across XY plane
                    transform.Translate(touchDeltaPosition.x * speed, 0, 0);
    
                    Vector3 boundaryVector = transform.position;   
                    boundaryVector.x = Mathf.Clamp (boundaryVector.x, -5.5f, -2.8f);
                    transform.position = boundaryVector;
                }
            }
            else
            {
                touched  = false;
            }
        }
    }
    

    希望对您有所帮助,

    【讨论】:

    • 感谢您的评论,我使用了此代码,但我收到与光线投射相关的错误?我该如何解决这个问题。还有我必须做些什么来处理其余的触摸而不是第一个。我是团结的新手,所以如果你能指出我正确的方向,我将不胜感激
    • 我修复了提供的代码错误(主要是因为我在这里写了它并没有测试它)并测试它:它工作正常。确保您在触摸设备上对其进行测试:由于使用了Input.GetTouch(),因此无法通过鼠标单击来工作。关于让它与其余的接触一起工作,我只是将touched bool 逻辑转换为 bool 数组。
    • 是的,它的工作。现在有了这个,当我选择主立方体时,我只能左右移动立方体,它是游戏对象的父级,我怎么能左右移动游戏对象但是当我触摸游戏对象的孩子时。我猜你必须使用 GetComponentsInChildren?
    • 我真的不明白您想要实现什么以及您的场景层次结构是如何组织的:您能否发布它的屏幕以及您的目标的图形说明?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多