【问题标题】:How can i raise the wall in unity3d once and control the raising speed?如何在unity3d中一次升起墙壁并控制升起速度?
【发布时间】: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));

步骤:

  1. 当玩家在 DetectPlayer 脚本中触摸对象时,在 RaiseWalls 脚本中以特定速度抬起墙壁。

  2. 当墙壁升起时,才开始启动协程。

【问题讨论】:

    标签: c# unity3d unity5


    【解决方案1】:

    所以,你想总共升起 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2020-07-21
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      相关资源
      最近更新 更多