【问题标题】:How can I correct my spinning platform script and fix the wrong texture in Unity?如何更正我的旋转平台脚本并修复 Unity 中的错误纹理?
【发布时间】:2021-07-26 20:53:34
【问题描述】:

我目前正在以初学者的身份制作 2D 游戏,有人为我制作了一个用于旋转平台的脚本,该脚本在特定秒数后停止。但它有错误的质地。它总是颠倒的,即使它转过一次。就像在屏幕截图上一样。除此之外,关于平台的两件事不再适用于他制作的脚本。旋转速度和顺时针旋转。我将把两个不同的脚本放在屏幕截图下方。我希望你能理解这一点,如果你有任何问题,请随时提出。

谢谢!

截图:

之前的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spinning_Platform : MonoBehaviour
{
    private float rotZ;
    public float RotationSpeed;
    public bool ClockwiseRotation;

    void Update()
    {
        if (ClockwiseRotation==false)
        {
            rotZ += Time.deltaTime * RotationSpeed;
        }
        else
        {
            rotZ += -Time.deltaTime * RotationSpeed;
        }

        transform.rotation = Quaternion.Euler(0, 0, rotZ);
    }
}

脚本之后:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spinning_Platform : MonoBehaviour
{
    public float rotZ;
    public float RotationSpeed;
    public bool ClockwiseRotation;
    public float time = 1;

    void Update()
    {
        time -= Time.deltaTime;
        if (time <= 0)
        {
            rotZ += 10;
            transform.rotation = Quaternion.Euler(0, 0, rotZ);
            if (rotZ == 180)
            {
                rotZ = 0;
                time = 1;
            }
        }
    }
}

【问题讨论】:

    标签: c# unity3d rotation


    【解决方案1】:

    新脚本有很多缺陷

    • using == for float value
    • frame-rate dependent rotation 每帧 10°。所以基本上它需要大约 36 帧来执行一个完整的旋转..假设 60fps 这几乎是每秒两个完整的旋转
    • 在到达180 时只需重置旋转...为什么?
    • 它实际上一秒钟内什么都不做,然后旋转 - 据我了解,您宁愿在一秒钟内旋转然后蹲下

    我会使用你的第一种方法,只添加时间计数器

    public class Spinning_Platform : MonoBehaviour
    {
        public float RotationSpeed;
        public bool ClockwiseRotation;
    
        // How long to rotate in seconds
        public float Duration = 1f;
        // Should this object be spinning right from the beginning?
        public bool startsSpinning = true;
    
        private float timer;
    
        private void Start ()
        {
            if(startsSpinning) Spin();
        }
    
        void Update()
        {
            // Rotate as long as the timer is not expired
            if(timer <= 0) return;
    
            // reduce the timer by time passed since last frame
            timer -= Time.deltaTime;
    
            // rotate the object on the global Z axis
            transform.Rotate(0, 0, (ClockwiseRotation ? 1 : -1) * RotationSpeed * Time.deltaTime, Space.World);
        }
    
        // This way you can also disable the spinning from start
        // and/or spin the object (again) via code
        public void Spin()
        {
            timer = Duration;
        }
    }
    

    编辑

    因为我只知道您的其他问题,您的平台实际上是Rigidbody2D,所以您不应该使用Transform 组件,而应该使用类似

    [SerializeField] private Rigidbody2D _rigidbody;
    
    private void Awake ()
    {
        if(!_rigidbody) _rigidbody = GetComponent<Rigidbody2D>();
    }
    
    // Remove the Update entirely
    
    void FixedUpdate()
    {
        // Rotate as long as the timer is not expired
        if(timer <= 0) return;
    
        // reduce the timer by time passed since last frame
        timer -= Time.deltaTime;
    
        // rotate the object on the global Z axis
        _rigidbody.MoveRotation(_rigidbody.rotation + (ClockwiseRotation ? 1 : -1) * RotationSpeed * Time.deltaTime);
    }
    

    【讨论】:

    • 哦,谢谢,但现在还有另一个: Assets/Spinning_Platform.cs(22,10): error CS0111: Type 'Spinning_Platform' 已经定义了一个名为 'Update' 的成员,具有相同的参数类型.对我来说再次没有意义xd
    • 哦,这很简单;)正如错误所说,您现在复制了 Update 方法...但是您的代码中已经有另一个..您只需要其中一个 ;)
    • 我是完全愚蠢还是为什么我看不到它?
    • 使用搜索查找方法更新...真的就是这么简单
    • @Sorceri 有道理,但是这个脚本中只有一种更新方法。和上面一样
    猜你喜欢
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多