C# 如何更正我的旋转平台脚本并修复Unity中的错误纹理?

C# 如何更正我的旋转平台脚本并修复Unity中的错误纹理?,c#,unity3d,rotation,C#,Unity3d,Rotation,作为一个初学者,我目前正在制作一个2D游戏,有人给我制作了一个旋转平台的脚本,在特定的秒数后停止。但它的质地不对。它总是颠倒的,即使它转了一圈。就像截图上一样。除此之外,平台的两个方面不再适用于他制作的脚本。旋转速度和顺时针旋转。我将把两个不同的脚本放在屏幕截图下面。我希望你能理解这一点,如果你有任何问题,请随时提问 谢谢 截图: 之前的脚本: using System.Collections; using System.Collections.Generic; using UnityEngin

作为一个初学者,我目前正在制作一个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;
            }
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类旋转平台:MonoBehavior
{
公共浮动旋转木马;
公众浮标轮换速度;
公共图书馆顺时针旋转;
公共浮动时间=1;
无效更新()
{
time-=time.deltaTime;

如果(时间新脚本中有很多缺陷

  • 每帧10°。所以基本上需要36帧来执行一次完整的旋转。假设每秒60帧,这几乎是每秒两次完整的旋转
  • 当达到
    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;
    }
}

哦,伙计,谢谢,但现在还有另一个:Assets/Spinning_Platform.cs(22,10):错误CS0111:Type'Spinning_Platform'已经用相同的参数类型定义了一个名为'Update'的成员。这对我来说同样没有意义xdOh这是一个简单的;)正如错误所说,您现在复制了
Update
方法…但是您的代码中已经有了另一个方法..您只需要其中一个;)我是完全愚蠢还是为什么看不到它?使用搜索来查找方法更新…真的是这样吗simple@Sorceri这是有道理的,但是这个脚本中只有一个更新方法同上
[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);
}