1

I'm currently making a 2D game as a beginner and someone made me a script for a spinning platform, which is stopping after a specific amount of seconds. But it has the wrong texture. It's always upside down, even if it turned one time. like on the screenshot. Besides that, two things about the platform aren't working anymore with the script he made. The rotation speed and the clockwise rotation. I will put the two different scripts below the screenshot. I hope you can understand this, feel free to ask if you have any questions.

Thanks!

Screenshot: enter image description here

script before:

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);
    }
}

script after:

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;
            }
        }
    }
}
JonathanXD12
  • 39
  • 1
  • 7

1 Answers1

1

There are a lot flaws in the new script

  • using == for float value
  • frame-rate dependent rotation with 10° per frame. So basically it takes about 36 frames to perform a full rotation .. assuming 60fps this is almost two full rotations per second
  • simply resetting the rotation when reaching 180 ... Why?
  • it actually does nothing for a second, then spins - as I understand you rather want to spin during a second and then stoo

I would use your first approach and only add the time counter

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;
    }
}

Edit

Because I just know fr your other question your platform actually is a Rigidbody2D so you shouldn't use the Transform component but rather something like

[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);
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Oh man, thanks, but now there's another one: Assets/Spinning_Platform.cs(22,10): error CS0111: Type 'Spinning_Platform' already defines a member called 'Update' with the same parameter types. Makes again no sense for me xd – JonathanXD12 May 04 '21 at 15:36
  • Oh that's an easy one ;) as the error says you now copied that `Update` method ... but there already is another one in your code .. you only want one of them ;) – derHugo May 04 '21 at 15:38
  • Am I completely dumb or why do I don't see it? – JonathanXD12 May 04 '21 at 15:41
  • use the search to find the method Update... it really is that simple – Sorceri May 04 '21 at 16:13
  • @Sorceri Makes sense, but there is only one update method in this script. It's just the same as above – JonathanXD12 May 04 '21 at 16:18
  • @derHugo Did I misunderstood this because my platform only has a transform component – JonathanXD12 May 04 '21 at 16:22