如何更改每个游戏对象的移动速度?

在层次结构中我有2个ThirdPersonController。 在Window> Animator中,我创建了一个名为Walk的新空状态并将其设置为HumanoidWalk,因此在运行游戏时,两个玩家都在行走。

在其中一个我添加了脚本和预制第二个ThirdPersonController(1)。

然后在运行游戏时,它正在制作ThirdPersonController(1)的克隆。 所以我在层次结构中看到更多N ThirdPerson控制器。

今天为了改变每个ThirdPersonController的行走速度,我在Inspector中更改了Move Speed Multiplier。 但是,如果我想在脚本中创建克隆时设置为每个另一个速度,我该怎么办呢?

using UnityEngine; using System.Collections; public class Multiple_objects : MonoBehaviour { public GameObject prefab; public GameObject[] gos; public int NumberOfObjects; void Awake() { gos = new GameObject[NumberOfObjects]; for(int i = 0; i < gos.Length; i++) { GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity); gos [i] = clone; } } // Use this for initialization void Start () { } // Update is called once per frame void Update () { } } 

我现在尝试的是获取Prefab的Animator组件并将速度设置为所有克隆:

 using UnityEngine; using System.Collections; public class Multiple_objects : MonoBehaviour { public GameObject prefab; public GameObject[] gos; public int NumberOfObjects; private Animator _animaotr; void Awake() { gos = new GameObject[NumberOfObjects]; for(int i = 0; i < gos.Length; i++) { GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity); gos [i] = clone; _animaotr.speed = 10; } } // Use this for initialization void Start () { _animaotr = prefab.GetComponent (); } // Update is called once per frame void Update () { } } 

但主要的问题是,在我的第一个层次结构中的ThirdPersonController中,我在Window> Animator中创建的原始空状态称为Walk并设置HumandoidWalk。

现在设置速度由于某种原因改变动画师的速度永远不会影响任何东西,例如:

 _animaotr.speed = 10; 

仅在更改ThirdPersonController> Inspector>第三人称角色(脚本)>移动速度倍增器中的速度时。 并且它正在改变层次结构中所有ThirdPerson控制器的速度,包括此克隆。

但是,我如何将每个克隆速度更改为另一个速度? 为什么_animator.speed没有改变任何东西,我需要使用这个移动速度倍增器?

在Editor中显示的Move Speed Multiplier属性在ThirdPersonCharacter脚本中声明为m_MoveSpeedMultiplier 。 它是delacre,因为float m_MoveSpeedMultiplier = 1f; 这意味着它是一个private变量, 无法从另一个脚本访问。 它出现在编辑器中的原因是因为它上面有[SerializeField] ,这意味着它是一个序列化的private变量。

要在运行时访问它,您必须从float m_MoveSpeedMultiplier = 1f;更改float m_MoveSpeedMultiplier = 1f; public float m_MoveSpeedMultiplier = 1f; ThirdPersonCharacter脚本中。

使用GetComponentGetComponent获取GetComponent实例,然后将其保存到某个地方以便重新使用。 由于您有2个ThirdPersonCharacter ,因此可以创建两个ThirdPersonCharacter数组来保存这些实例。 它应该看起来像下面的代码:

 using UnityEngine; using System.Collections; using UnityStandardAssets.Characters.ThirdPerson; public class Multiple_objects : MonoBehaviour { public GameObject prefab; public GameObject[] gos; public int NumberOfObjects; private ThirdPersonCharacter[] thirdPersonCharacter; void Awake() { thirdPersonCharacter = new ThirdPersonCharacter[2]; gos = new GameObject[NumberOfObjects]; for (int i = 0; i < gos.Length; i++) { GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity); gos[i] = clone; thirdPersonCharacter[i] = clone.GetComponent(); } } // Use this for initialization void Start() { thirdPersonCharacter[0].m_MoveSpeedMultiplier = 5f; thirdPersonCharacter[1].m_MoveSpeedMultiplier = 5f; } // Update is called once per frame void Update() { } }