C# 触摸控制可左右移动立方体(在生成立方体的阵列中)
我正在移动一个由两个立方体组成的对象:左立方体和右立方体。这些立方体在y轴向上随机生成 我可以毫无问题地左右移动它们,但是,当我向左或向右移动其中一个立方体时,它们都会移动 当只向左或向右触摸而不是全部触摸时,我如何才能只移动其中一个立方体?下面是我的代码: 生成多维数据集代码:C# 触摸控制可左右移动立方体(在生成立方体的阵列中),c#,ios,unity3d,C#,Ios,Unity3d,我正在移动一个由两个立方体组成的对象:左立方体和右立方体。这些立方体在y轴向上随机生成 我可以毫无问题地左右移动它们,但是,当我向左或向右移动其中一个立方体时,它们都会移动 当只向左或向右触摸而不是全部触摸时,我如何才能只移动其中一个立方体?下面是我的代码: 生成多维数据集代码: public Transform block; public Transform player; private float objectSpawnedTo = 5.0f; public static float di
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;
}
}
问题是您的多维数据集控制代码中没有关于选择哪个多维数据集的条件:您只需等待
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;
}
}
}
[序列化字段]
专用浮动速度=0.5f;
私人int最大触摸计数=5;
私人住宅;
受保护的void Start()
{
触摸=新布尔[最大触摸计数];
}
无效更新()
{
如果(Input.touchCount>0)
{
对于(int i=0;i<(Input.touchCount感谢您的评论,我使用了此代码,但我遇到了与raycast相关的错误?我如何解决此问题。我还需要做些什么来处理其余的触摸而不是第一个。我是unity新手,因此如果您能为我指出正确的方向,我将非常感谢您修复了提供的代码错误(主要是因为我在这里写的,没有测试过)并测试过:它工作正常。请确保您在触摸设备上测试它:由于Input.GetTouch()使用了
。关于让它与其他触摸一起工作,我只是将
触摸bool逻辑转换为bool数组。是的,它起作用了。现在,当我选择作为游戏对象右侧父对象的主多维数据集时,我只能左右移动多维数据集,我如何才能左右移动游戏对象,但当我触摸到游戏对象的子对象。我猜你必须使用GetComponentsInChildren?我真的不明白你想要实现什么以及你的场景层次结构是如何组织的:你能发布一个屏幕,也许还有一个你目标的图形说明吗?
[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;
}
}
}