- Home /
Spinning platform C# script correction & wrong texture
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:
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;
}
}
}
}
Answer by jackmw94 · May 04, 2021 at 02:42 PM
Your first script looks like it does the clockwise / anticlockwise rotation but has no timer and the second script does not account for the speed or direction and the floating point comparison worries me too.
I think your second script is the best starting point to get all features working here. Firstly, to account for speed and direction, your line 17 should look more like the rotZ assignment in your first script:
rotZ += ClockwiseRotation ? -Time.deltaTime * RotationSpeed : Time.deltaTime * RotationSpeed;
The question mark (called the conditional operator) is doing the same thing the if statement was doing: if it's true then it evaluates to the value before the colon, if it's false then it's the value after the colon. Notice this is exactly what's happening in the first script but we can put it in the second script to keep the timer code.
Finally, since your rotZ is a float it's good practice to treat it as though at any time it could be +/- 0.0000001 what you think it might be so although you might expect it to be 180 when we get to the "== 180" if statement, it could actually be 179.999999998 because it's a floating point value (and therefore a little rascal). Also since this now accounts for your speed, this might not increment in nice round numbers and could go from 179.5 one frame to 180.5 the next and therefore that check will never evaluate to true. It might be better if you check whether your rotation is GREATER than 180 and if so MINUS 180 from it. So if it does go from 179.5 to 180.5, it will return to 0.5 because you've taken 180 from it.
I'm not sure why you're resetting time to 1 when you get to 180 degrees. This is making me wonder whether there's something I've misunderstood about what you're trying to do.. As far as I'm aware, I'd make that section look like:
if (rotZ > 180f)
{
rotZ -= 180f;
}
transform.rotation = Quaternion.Euler(0f, 0f, rotZ);
Let me know if I have misunderstood anything and I'll try help with that :)
@jackmw94 Thank you for your answer, so the script should now look like this right?
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 += ClockwiseRotation ? -Time.deltaTime * RotationSpeed : Time.deltaTime * RotationSpeed;
if (rotZ > 180f)
{
rotZ -= 180f;
}
transform.rotation = Quaternion.Euler(0f, 0f, rotZ);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613033256im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving and scaling an object according to hand position. 1 Answer
How do I attach a Created Material A Texture, NormalMap and OcclussionMap? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
UV mapping icosphere 1 Answer