【发布时间】:2017-02-17 00:48:27
【问题描述】:
触摸对象和播放动画的部分工作正常,现在我想添加墙壁脚本部分。
在这种情况下,我更改了立方体的高度。 我需要做的是,只有当玩家触摸一个物体时,它才会升高/改变另一个物体的高度。
儿子在第一种情况下,我发现玩家触摸物体时:
using UnityEngine;
using System.Collections;
using System.Reflection;
public class DetectPlayer : MonoBehaviour {
GameObject target;
int counter = 0;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "ThirdPersonController") // "Platform"
{
Debug.Log("Touching Platform");
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "ThirdPersonController") // "OnTop Detector"
{
counter = 0;
Debug.Log("On Top of Platform");
target = GameObject.Find("Elevator");
GameObject findGo = GameObject.Find("ThirdPersonController");
GameObject findGo1 = GameObject.Find("Elevator");
findGo.transform.parent = findGo1.transform;
GameObject go = GameObject.Find("CubeToRaise");
go.GetComponent<RaiseWalls>();
Debug.Log("The button clicked, raising the wall");
StartCoroutine(playAnim(target));
}
}
void OnTriggerExit(Collider other)
{
GameObject findGo = GameObject.Find("ThirdPersonController");
findGo.transform.parent = null;
}
IEnumerator playAnim(GameObject target)
{
Animation anim = target.GetComponent<Animation>();
foreach (AnimationState clip in anim)
{
// do initialisation or something on clip
clip.speed = 1;
}
while (true)
{
if (counter == 1)
break;
anim.Play("Up");
while (anim.IsPlaying("Up"))
{
yield return null;
}
anim.Play("Down");
while (anim.IsPlaying("Down"))
{
yield return null;
}
yield return null;
counter++;
}
}
void OnGUI()
{
GUI.Box(new Rect(300, 300, 200, 20),
"Times lift moved up and down " + counter);
}
}
在这部分,我正在调用第二个脚本 RaiseWalls:
using UnityEngine;
using System.Collections;
public class RaiseWalls : MonoBehaviour
{
public GameObject gameObjectToRaise;
public float speed;
// Use this for initialization
void Start()
{
speed = 2;
}
void Update()
{
gameObjectToRaise.transform.localScale += new Vector3(0, 50, 0);
}
}
GameObject go = GameObject.Find("CubeToRaise");
go.GetComponent<RaiseWalls>();
Debug.Log("The button clicked, raising the wall");
现在 DetectPlayer 已附加到一个游戏对象上。 RaiseWalls 脚本附加在另一个游戏对象上。
在 RaiseWalls 脚本上,我想设置对象高度变化的速度。现在它改变了 50 倍的高度,但是很多次。我希望它改变 50 倍,但像缓慢建造/升起的墙一样慢动作。就像电子围栏从下向上的效果一样。
第二个问题我希望首先它会在完成提升墙壁后提升墙壁移动到 DetectPlayer 脚本中的下一部分:
StartCoroutine(playAnim(target));
步骤:
当玩家在 DetectPlayer 脚本中触摸对象时,在 RaiseWalls 脚本中以特定速度抬起墙壁。
当墙壁升起时,才开始启动协程。
【问题讨论】: