【问题标题】:Unity - cant get objects to vibrate/hover in place?Unity - 无法让物体振动/悬停在原地?
【发布时间】:2018-12-05 21:26:34
【问题描述】:

好的,我有一堆立方体,我只需要在太空中缓慢振动/摇晃就好像几乎悬停一样。我首先尝试使用带有噪声的粒子发射器,但无法获得运动部分。我现在正在尝试将脚本附加到每个对象:

 float speed = 1.0f; //how fast it shakes
    float amount = 1.0f;
   public  Vector2 startingPos;

    void Awake()
    {
        startingPos.x = transform.position.x;
        startingPos.y = transform.position.y;
    }

    // Update is called once per frame
    void Update()
    {

        float newx = startingPos.x + (Mathf.Sin(Time.time * speed) * amount);
        float newy = startingPos.y + (Mathf.Sin(Time.time * speed) * amount);

        transform.position = new Vector3(newx, newy, transform.position.z);

    }

这样,立方体就完全消失了。我在论坛上查看无济于事 - 我该怎么做才能让立方体振动/悬停在原地?

【问题讨论】:

    标签: c# unity3d game-physics game-development


    【解决方案1】:

    理想情况下,您应该使用诸如LeanTween 之类的包来处理您的过渡/动画,至少在这样的简单、小规模情况下是这样。

    本质上,流程是这样的:

    On Start:
        store 'starting' position
    
    On Enabled:
        Run DoHover
    
    On DoHover:
        Pick random offset within radius (Random.insideUnitCircle() * radius)
        LeanTween current transform to startingPos + offset over timeDelay
        Set LeanTween.OnComplete to run DoHover
    

    如果你确定不使用 3rd-party 包来管理补间,你可以试试这个(我使用 3D 场景,但你应该可以轻松地将其展平):

        private const float threshold = 0.02f;
    
        private Vector3 startingPos;
        private Vector3 destinationPos;
        private Vector3 velocity;
        public float radius = 0.1f;
        public float minTimeDelay = 0.1f;
        public float maxTimeDelay = 0.3f;
    
        private void Start() {
            startingPos = destinationPos = transform.position;
        }
    
    
        // This is a 'physics' frame update
        public void FixedUpdate() {
    
            if ( Vector3.Distance(transform.position, destinationPos) < threshold ) {
                // Pick new destination and speed
                destinationPos = startingPos + Random.insideUnitSphere * radius;
                velocity = (destinationPos - transform.position) / Random.Range(minTimeDelay, maxTimeDelay);
            }
    
            // Move toward destination
            transform.position += velocity * Time.fixedDeltaTime;
        }
    

    【讨论】:

    • 我真的很想避免使用外部软件包,因为我已经安装了 iTween,并且只在这种情况下需要它。有没有外包装的shake的代码示例?
    • 您当然可以编写自己的补间代码,可能在协程中,但我绝对建议您坚持使用 3pp 包。如果您喜欢 iTween 就可以了,LeanTween 更像是 C# 风格。我会看看我能不能想出一些没有中间件的东西。
    【解决方案2】:

    我今天使用三个堆叠的 Sin 波解决了这个问题;它很简单,并且提供了一个很好的悬停随机围绕一个圆圈的效果,并且不需要跟踪对象的位置/曾经:

    transform.position.x = originalPosition.x + hoverSpeed * (
        Mathf.Sin(Time.time / 3) 
        + Mathf.Sin(Time.time / 7) 
        + Mathf.Sin(Time.time / 11)
        );
    
    transform.position.y = originalPosition.y + hoverSpeed * (
        Mathf.Sin(Time.time / 2) 
        + Mathf.Sin(Time.time / 5) 
        + Mathf.Sin(Time.time / 13)
        );

    我还从 originalPosition 开始对象,然后通过将 hoverSpeed 设置为零并执行以下操作逐渐向外移动:

    hoverSpeed = Mathf.Clamp(hoverSpeed + Time.deltaTime, 0, hoverMaxSpeed);

    【讨论】:

      猜你喜欢
      • 2020-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      相关资源
      最近更新 更多