【发布时间】:2016-07-31 03:31:30
【问题描述】:
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.Characters.ThirdPerson;
public class Multiple_objects : MonoBehaviour {
public GameObject prefab;
public GameObject[] gos;
public int NumberOfObjects;
private ThirdPersonCharacter[] thirdPersonCharacter;
private Animator[] _animator;
private int count = 0;
private List<float> floats = new List<float>();
public float smooth = 1f;
private Vector3 targetAngles;
void Awake()
{
Vector3 v3 = prefab.transform.position;
_animator = new Animator[NumberOfObjects];
gos = new GameObject[NumberOfObjects];
for(int i = 0; i < gos.Length; i++)
{
count = count + 2;
GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
gos [i] = clone;
gos [i].transform.position = new Vector3 (v3.x - count, v3.y, v3.z);
_animator [i] = gos[i].GetComponent<Animator> ();
float randomspeed = 0f;
// Keep repeating this until we find an unique randomspeed.
while(randomspeed == 0f || floats.Contains(randomspeed))
{
randomspeed = UnityEngine.Random.Range(1.0f, 15.0f);
}
floats.Add (randomspeed);
//float vertInput = 1.0f;
_animator [i].SetFloat ("Speed", randomspeed);
if (randomspeed != 0.0f)
_animator[i].speed = randomspeed;
else
_animator [i].speed = 1.0f;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.S)) // some condition to rotate 180
targetAngles = gos[5].transform.eulerAngles + 180f * Vector3.up; // what the new angles should be
transform.eulerAngles = Vector3.Lerp(gos[5].transform.eulerAngles, targetAngles, smooth * Time.deltaTime); // lerp to new angles
}
}
在层次结构中,我有两个 ThirdPersoncontroller:ThirdPersonController 和 ThirdPersonController(1)
我将脚本放在 ThirdPersonController 上,预制件是 ThirdPersonController(1) 然后它克隆了更多 15 个 ThirdPersonController。
然后克隆的 15 名玩家中的每一个都以另一种速度行走。 现在在更新功能中,当我按下 S 键时,ThirdPersonController 旋转 180 度,然后所有克隆的 15 名玩家也旋转 180 度并继续朝这个方向走。
我想要做的是,当其中一个克隆玩家到达 Z 位置时,仅此玩家转动 180 度,然后在原地 180 度转一秒钟,然后两个在原地转弯,然后继续向转向的方向行走。
p>所以在更新功能中,我现在使用 S 键作为条件,但它正在转动 ThirdPersonController,所有克隆都根据他转动并走向转动的方向。
我尝试使用 gos[0].transform 也使用 gos[5].transform 但它不仅转动第五个克隆,而且将它们全部转动 180 度。
而我试图做出的条件是 if(gos[0].transform.position.z == 108.9377f) 或 if(gos[0].transform.position.z == 100) 但它从来没有达到下一行我使用了一个断点。
【问题讨论】:
标签: c# unity3d unityscript unity5