using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpinObject : MonoBehaviour
{
public float rotationSpeed;
public int slowdown;
public GameObject[] objectsToRotate;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
for (int i = 0; i < objectsToRotate.Length; i++)
{
if ((int)System.Math.Ceiling(rotationSpeed) >= slowdown)
{
rotationSpeed -= 0.1f;
}
else
{
rotationSpeed += 0.1f;
}
objectsToRotate[i].transform.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
}
The problem is for example if in the inspector i set the Rotation Speed to 50 and the Slowdown to 500 when the speed is getting to 499.0215 then it’s staying on this speed it’s not starting slowdown since it’s not getting to 500.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpinObject : MonoBehaviour
{
private bool slowDown = false;
public float rotationMultiplier;
public float slowdown;
public GameObject[] objectsToRotate;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
for (int i = 0; i < objectsToRotate.Length; i++)
{
if (rotationMultiplier > 500)
slowDown = true;
else if (rotationMultiplier < 0)
slowDown = false;
rotationMultiplier = (slowDown) ? rotationMultiplier - 0.1f : rotationMultiplier + 0.1f;
objectsToRotate[i].transform.Rotate(Vector3.forward, Time.deltaTime * rotationMultiplier);
}
}
}
So if i set for example the slowdown to 50 when it’s getting to 500 it will start slow down when it get to 50 it will start move faster up and so on.
I don’t really understand what you are trying to accomplish…
If you want the object to stop accelerating after you start slowing it down, just add another bool called accelerate and before the accelerating code check if it’s set to true.
Then just set it to false before the for loop and it should now stop accelerating.
Also, it’s a pretty bad idea to have a for loop in the update function (as the function is called every frame and the for loop would be activated every frame lowering the fps drastically). Try to put it in an if statement like:
if (slowDown == true) {
//For loop
}
This is a working solution. But what should i do with the loop(FOR) in the Update ?
And how can i use the global variables and make that if a gameobject in the array options are empty for example if a gameobject option maxSpeed is 0 then use the global variable slowdownMax.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SpinableObject
{
[SerializeField]
private Transform t;
[SerializeField]
private float rotationSpeed;
[SerializeField]
private float minSpeed;
[SerializeField]
private float maxSpeed;
[SerializeField]
private float speedRate;
private bool slowDown;
public void Rotate()
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
public class SpinObject : MonoBehaviour
{
private bool slowDown = false;
public float rotationSpeed;
public float slowdownMax;
public float slowdownMin;
public SpinableObject[] objectsToRotate;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < objectsToRotate.Length; i++)
{
objectsToRotate[i].Rotate();
}
}
}
I want that this variables:
public float rotationSpeed;
public float slowdownMax;
public float slowdownMin;
Will effect all the gameobject array. All the gameobjects.
But if one of the gameobjects options is filled then the global effect will not work on this specific gameobject option.
For example if in the array gameobject[0] maxSpeed is set to 0 use the global slowdownMax.
But if gameobject[0] maxSpeed is not set to 0 then use this gameobject maxSpeed and not the global slowdownMax.