JonathanXD12
JonathanXD12

Reputation: 61

How can I correct my spinning platform script and fix the wrong texture in Unity?

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

Upvotes: 1

Views: 120

Answers (1)

derHugo
derHugo

Reputation: 90580

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

Upvotes: 1

Related Questions