The script behavior now is when the camera inside the method StartCameraRotation is reaching the maximum rotation speed to 180 then it’s at once changing the rotatearound direction keep rotating that direction for x seconds then change direction and again keep rotating around for that direction for x seconds and so on a ping pong.
but I want some different behavior, I want that when the camera rotate around is reaching the maximum rotation speed of 180 then it will wait for example 5 seconds or so using timeToChangeDirection keep rotating around at this speed the same direction and after 5 seconds start at the same time smooth rotate to the other direction and also to slow down decrease the speed to 0 using this variable timeToReachMaxSpeed so it will take to speed up and slow down the same time. then when the speed is reaching to 0 wait again some seconds using timeToChangeDirection then start changing direction to the other direction and also smooth slowly speed up again back to 180. and so on. this kind of ping pong.
so in general , the behavior should be ping pong between the directions and speed of rotation with staying at each point some x seconds. stay at speed 0 timeToChangeDirection and then when reaching to 180 stay for timeToChangeDirection before changing direction and speed.
using System.Collections;
using TMPro;
using UnityEngine;
public class BallManager : MonoBehaviour
{
public GameObject ballPrefab;
public int numberOfBalls = 10;
public float fallSpeed = 5;
public float spawningSpeed = 1;
public float ballSize = 1f;
public bool randomSize = false;
public TextMeshProUGUI ballCountText; // Reference to the TextMeshPro Text
public float maxRotationSpeed = 180.0f;
public float rotationSpeed = 10.0f; // Initial rotation speed (when it starts)
public float timeToReachMaxSpeed = 30.0f; // Time in seconds to reach maximum rotation speed
public float timeToChangeDirection = 5.0f;
public float currentRotationSpeed; // Variable to display the current rotation speed in the inspector
private int ballCount; // Counter for the number of balls
private Transform cameraTransform;
private void Start()
{
cameraTransform = Camera.main.transform;
currentRotationSpeed = rotationSpeed; // Initialize currentRotationSpeed
StartCoroutine(SpawnBalls());
StartCoroutine(StartCameraRotation());
}
private IEnumerator SpawnBalls()
{
for (int i = 0; i < numberOfBalls; i++)
{
GameObject ball = Instantiate(ballPrefab, transform.position, Quaternion.identity);
if (randomSize)
{
float randomScale = Random.Range(0.5f, 2.0f); // Random size between 0.5 and 2
ball.transform.localScale = new Vector3(randomScale, randomScale, randomScale);
}
else
{
ball.transform.localScale = new Vector3(ballSize, ballSize, ballSize);
}
Rigidbody rb = ball.GetComponent<Rigidbody>();
rb.velocity = Vector3.down * fallSpeed;
// Randomize bouncing direction
Vector3 randomDirection = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)).normalized;
rb.AddForce(randomDirection * 5f, ForceMode.Impulse);
// Increase the ball count and update the TextMeshPro Text
ballCount++;
ballCountText.text = "Balls: " + ballCount;
yield return new WaitForSeconds(spawningSpeed); // Time delay between spawning balls
}
}
private IEnumerator StartCameraRotation()
{
float timer = 0.0f;
while (timer < timeToReachMaxSpeed)
{
timer += Time.deltaTime;
currentRotationSpeed = Mathf.Lerp(0, maxRotationSpeed, timer / timeToReachMaxSpeed);
// Rotate the camera around the target
cameraTransform.RotateAround(transform.position, Vector3.up, currentRotationSpeed * Time.deltaTime);
yield return null;
}
// Now, keep rotating at the maximum speed
while (true)
{
cameraTransform.RotateAround(transform.position, Vector3.up, maxRotationSpeed * Time.deltaTime);
// If it's time to change direction
if (timer > timeToChangeDirection)
{
// Reset the timer and reverse the rotation direction
timer = 0.0f;
maxRotationSpeed *= -1;
}
timer += Time.deltaTime;
yield return null;
}
}
}