我怎样才能改变每个游戏对象的移动速度?

2024-02-06

在层次结构中我有 2 ThirdPersonController。 在“窗口”>“动画器”中,我创建了新的空状态,将其称为“步行”并将其设置为“HumanoidWalk”,以便在运行游戏时两个玩家都在步行。

在其中一个上,我添加了脚本并将第二个 ThirdPersonController(1) 作为预制件。

然后,当运行游戏时,它会克隆 ThirdPersonController(1)。 所以我在层次结构中看到更多 N ThirdPersoncontrollers。

今天,为了更改每个 ThirdPersonController 的行走速度,我在检查器中更改了移动速度倍增器。 但是,如果我想在创建克隆时在脚本中设置彼此的速度,我该怎么办?

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 () {

    }
}

我现在尝试的是获取预制件的 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<Animator> ();

    }

    // Update is called once per frame
    void Update () {

    }
}

但主要问题是,在层次结构中的第一个 ThirdPersonController 上,我在“窗口”>“动画器”空状态中创建的原始控制器将其称为“Walk”并设置“HumandoidWalk”。

现在,由于某种原因设置速度,改变动画器速度不会产生任何影响,例如:

_animaotr.speed = 10;

仅当在 ThirdPersonController > Inspector > Third Person Character (Script) > Move Speed Multiplier 中更改速度时。它正在将相同的速度更改为层次结构中的所有 ThirdPerson 控制器,包括此 i 克隆。

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


The 移动速度倍增器编辑器中显示的属性声明为m_MoveSpeedMultiplier in the ThirdPersonCharacter脚本。它是德拉克作为float m_MoveSpeedMultiplier = 1f;这意味着它是一个private变量和cannot可以从另一个脚本访问。它出现在编辑器中的原因是因为它有[SerializeField]在它上面,这意味着它是一个序列化的private多变的。

要在运行时访问它,您必须从float m_MoveSpeedMultiplier = 1f; to public float m_MoveSpeedMultiplier = 1f; in the ThirdPersonCharacter script.

Use GetComponent获取实例ThirdPersonCharacter来自 gos GameObject 然后将其保存在某处以供重新使用。既然你有 2ThirdPersonCharacter,您可以创建两个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<ThirdPersonCharacter>();
        }
    }

    // Use this for initialization
    void Start()
    {

        thirdPersonCharacter[0].m_MoveSpeedMultiplier = 5f;
        thirdPersonCharacter[1].m_MoveSpeedMultiplier = 5f;
    }

    // Update is called once per frame
    void Update()
    {

    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我怎样才能改变每个游戏对象的移动速度? 的相关文章

随机推荐