如何更改每个游戏对象的移动速度? [英] How can i change each gameobject movement speed?
问题描述
在层次结构中,我有2个ThirdPersonController. 在窗口">动画制作器"中,我创建了一个名为行走"的新空状态,并将其设置为HumanoidWalk,因此在运行游戏时,两个玩家都在行走.
在其中一个上,我添加了脚本,第二个ThirdPersonController(1)作为Prefab.
然后,在运行游戏时,它会克隆ThirdPersonController(1). 因此,我在层次结构中看到了N个ThirdPersoncontroller.
如今,要更改每个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 () {
}
}
我现在尝试的是获取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<Animator> ();
}
// Update is called once per frame
void Update () {
}
}
但是主要的问题是,在层次结构中的第一个ThirdPersonController上,我在Window> Animator空状态中创建的原始名称为Walk并设置HumandoidWalk.
现在出于某些原因设置速度,例如,更改Animator速度永远不会影响任何东西
_animaotr.speed = 10;
仅当在ThirdPersonController>检查器>第三人称角色(脚本)>移动速度倍增器中更改速度时.而且,它正在对包括此i克隆在内的层次结构中的所有ThirdPersoncontroller更改相同的速度.
但是我如何将每个克隆速度更改为另一个速度?以及为什么_animator.speed不更改任何内容,而我需要使用此移动速度倍增器"?
在编辑器中显示的 Move Speed Multiplier 属性在ThirdPersonCharacter
脚本中声明为m_MoveSpeedMultiplier
.它以float m_MoveSpeedMultiplier = 1f;
表示,这意味着它是一个private
变量,并且无法可以从另一个脚本访问.它在编辑器中显示的原因是因为它上面有[SerializeField]
,这意味着它是序列化的private
变量.
要在运行时访问它,必须在ThirdPersonCharacter
脚本中从float m_MoveSpeedMultiplier = 1f;
更改为public float m_MoveSpeedMultiplier = 1f;
.
使用GetComponent
从gos GameObject获取ThirdPersonCharacter
的实例,然后将其保存在某处以备重复使用.由于您有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<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()
{
}
}
In the Hierarchy i have 2 ThirdPersonController. In the Window > Animator i created new empty state called it Walk and set it to HumanoidWalk so when running the game both players are walking.
On one of them i added the script and as Prefab the second ThirdPersonController(1).
Then when running the game it's making clones of the ThirdPersonController(1). So i see in the Hierarchy more N ThirdPersoncontrollers.
Today to change the speed of walking for each ThirdPersonController i change in the Inspector the Move Speed Multiplier. But if i want in the script already when creating the clones to set to each one another speed how can i do it ?
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 () {
}
}
What i tried now is to get the Animator component of the Prefab and set the speed to all the clones:
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 () {
}
}
But the main problem is that on my first ThirdPersonController in the Hierarchy the original one i created in Window > Animator empty state called it Walk and set HumandoidWalk.
Now to set the speed for some reason changing the Animator speed never effect of anything for example:
_animaotr.speed = 10;
Only when changing the speed in the ThirdPersonController > Inspector > Third Person Character (Script) > Move Speed Multiplier. And it's changing the same speed to all ThirdPersoncontrollers in the Hierarchy including this i clone.
But how do i change each clone speed to another vlaue of speed ? And why the _animator.speed not changing anything and i need to use this Move Speed Multiplier ?
The Move Speed Multiplier property that is showing in the Editor is declared as m_MoveSpeedMultiplier
in the ThirdPersonCharacter
script. It is delacre as float m_MoveSpeedMultiplier = 1f;
which means that it is a private
variable and cannot be accessed from another script. The reason it is showing up in the Editor is because it has [SerializeField]
on top of it which means that it is a serialized private
variable.
To access it during run-time, you have to change from float m_MoveSpeedMultiplier = 1f;
to public float m_MoveSpeedMultiplier = 1f;
in the ThirdPersonCharacter
script.
Use GetComponent
to get instance of ThirdPersonCharacter
from the gos GameObject then save it somewhere for re-usual. Since you have 2 ThirdPersonCharacter
, you can create two ThirdPersonCharacter
arrays to hold those instances. It should look like the code below:
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()
{
}
}
这篇关于如何更改每个游戏对象的移动速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!