如何让克隆体在 parent 移动时忽略移动输入
How to make a clone ignore movement input when his parent is moving
我用move_states来追踪角色的移动,但它只能追踪自己,当角色分裂成两个角色时,如何让克隆人在他的parent是时忽略玩家的输入移动。
脚本的 movement_input 部分:
private void Movement_input()
{
if (move_states == 0)
{
if (Input.GetKey(KeyCode.UpArrow))
{
move_states = 1;
target_position = transform.position + new Vector3(0, 0.8f, 0);
}
else if (Input.GetKey(KeyCode.DownArrow))
{
move_states = 1;
target_position = transform.position + new Vector3(0, -0.8f, 0);
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
move_states = 1;
target_position = transform.position + new Vector3(-1, 0, 0);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
move_states = 1;
target_position = transform.position + new Vector3(1, 0, 0);
}
}
}
感谢您的帮助!
如果我没理解错的话,你有一个可以克隆的object,它的脚本也被克隆了。一个简单的解决方案是禁用 child 的脚本 no?。
当您说 parent 时,您是指层次结构 parent 还是只是克隆的 object?在层次结构 parent 的情况下,您可以将 child 从其 parent.
中分离出来
我会将克隆的对象与另一个脚本一起制作成单独的预制件。从主脚本中,您可以获得对克隆的引用,并在每次它们应该移动时调用它们的方法。像这样,
private Gameobject[] copies;
private int dir;
public void MoveClones()
{
for (int i = 0; i < copies.Length; i++)
{
copies[i].SendMessage("MoveDir", dir);
}
}
然后您的 MoveDir 函数位于克隆对象脚本中,带有一个方向参数。你可以在那里查看它自己的移动状态。
你有一些选择。
例如:
我会分别实例化子项,并在每个子项(甚至是第一个)中使用标签(例如 "clone")。
然后,每次你试图移动其中一个(你必须想出一种方法来决定哪个优先)它必须检查一个循环,搜索带有标签 "clone" 的对象,如果有人有 move_states到 0。
部分代码可能是:
bool canMove = true;
for each (GameObject clone in GameObject.FindGameObjectsWithTag()){
if (clone.GetComponent<script>().move_states == 1)
canMove = false;
}
if(canMove == true)
Movement_input();
如果你成功了请告诉我;)
我用move_states来追踪角色的移动,但它只能追踪自己,当角色分裂成两个角色时,如何让克隆人在他的parent是时忽略玩家的输入移动。
脚本的 movement_input 部分:
private void Movement_input()
{
if (move_states == 0)
{
if (Input.GetKey(KeyCode.UpArrow))
{
move_states = 1;
target_position = transform.position + new Vector3(0, 0.8f, 0);
}
else if (Input.GetKey(KeyCode.DownArrow))
{
move_states = 1;
target_position = transform.position + new Vector3(0, -0.8f, 0);
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
move_states = 1;
target_position = transform.position + new Vector3(-1, 0, 0);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
move_states = 1;
target_position = transform.position + new Vector3(1, 0, 0);
}
}
}
感谢您的帮助!
如果我没理解错的话,你有一个可以克隆的object,它的脚本也被克隆了。一个简单的解决方案是禁用 child 的脚本 no?。
当您说 parent 时,您是指层次结构 parent 还是只是克隆的 object?在层次结构 parent 的情况下,您可以将 child 从其 parent.
中分离出来我会将克隆的对象与另一个脚本一起制作成单独的预制件。从主脚本中,您可以获得对克隆的引用,并在每次它们应该移动时调用它们的方法。像这样,
private Gameobject[] copies;
private int dir;
public void MoveClones()
{
for (int i = 0; i < copies.Length; i++)
{
copies[i].SendMessage("MoveDir", dir);
}
}
然后您的 MoveDir 函数位于克隆对象脚本中,带有一个方向参数。你可以在那里查看它自己的移动状态。
你有一些选择。
例如: 我会分别实例化子项,并在每个子项(甚至是第一个)中使用标签(例如 "clone")。 然后,每次你试图移动其中一个(你必须想出一种方法来决定哪个优先)它必须检查一个循环,搜索带有标签 "clone" 的对象,如果有人有 move_states到 0。
部分代码可能是:
bool canMove = true;
for each (GameObject clone in GameObject.FindGameObjectsWithTag()){
if (clone.GetComponent<script>().move_states == 1)
canMove = false;
}
if(canMove == true)
Movement_input();
如果你成功了请告诉我;)