我怎样才能在unity3d中升起一次墙并控制上升速度?
How can i raise the wall in unity3d once and control the raising speed?
触摸物体和播放动画的部分工作正常,现在我想添加墙壁脚本部分。
在这种情况下,我更改了立方体高度。
我需要做的是,只有当玩家触摸一个物体时,它才会 raise/change 另一个物体的高度。
儿子在第一种情况下我发现玩家正在触摸一个物体:
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 为单位进行更改,但要像缓慢 building/raising 墙一样进行慢动作。像电子围栏一样,从下往上提升效果。
第二个问题,我希望它首先会在完成升起墙壁后升起墙壁移动到 DetectPlayer 脚本中的下一部分:
StartCoroutine(playAnim(target));
步骤:
当玩家在 DetectPlayer 脚本中触摸物体时,以特定速度在 RaiseWalls 脚本中提升 wall/s。
当墙壁升起后才启动StartCoroutine。
所以,你想把墙总共升高 50,但速度可控。
首先在RaiseWalls中制作一个计数器:
float raiseAmount;
也记录下总筹码,方便以后修改:
float raiseTotal = 50;
然后,在你的更新中,提高一点,但记录提高了多少
if(raiseAmount < raiseTotal ) // i.e. we haven't raised it fully
{
// work out how much to raise it
float raiseThisFrame = speed * Time.DeltaTime; // to account for frame rate
// now we cap it to make sure it doesn't go over 50
if(raiseAmount + raiseThisFrame > raiseTotal )
{
raiseThisFrame = raiseTotal - raiseAmount;
}
// add raiseThisFrame to raiseAmount
raiseAmount += raiseThisFrame;
gameObjectToRaise.transform.localScale += new Vector3(0, raiseThisFrame, 0);
}
触摸物体和播放动画的部分工作正常,现在我想添加墙壁脚本部分。
在这种情况下,我更改了立方体高度。 我需要做的是,只有当玩家触摸一个物体时,它才会 raise/change 另一个物体的高度。
儿子在第一种情况下我发现玩家正在触摸一个物体:
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 为单位进行更改,但要像缓慢 building/raising 墙一样进行慢动作。像电子围栏一样,从下往上提升效果。
第二个问题,我希望它首先会在完成升起墙壁后升起墙壁移动到 DetectPlayer 脚本中的下一部分:
StartCoroutine(playAnim(target));
步骤:
当玩家在 DetectPlayer 脚本中触摸物体时,以特定速度在 RaiseWalls 脚本中提升 wall/s。
当墙壁升起后才启动StartCoroutine。
所以,你想把墙总共升高 50,但速度可控。
首先在RaiseWalls中制作一个计数器:
float raiseAmount;
也记录下总筹码,方便以后修改:
float raiseTotal = 50;
然后,在你的更新中,提高一点,但记录提高了多少
if(raiseAmount < raiseTotal ) // i.e. we haven't raised it fully
{
// work out how much to raise it
float raiseThisFrame = speed * Time.DeltaTime; // to account for frame rate
// now we cap it to make sure it doesn't go over 50
if(raiseAmount + raiseThisFrame > raiseTotal )
{
raiseThisFrame = raiseTotal - raiseAmount;
}
// add raiseThisFrame to raiseAmount
raiseAmount += raiseThisFrame;
gameObjectToRaise.transform.localScale += new Vector3(0, raiseThisFrame, 0);
}