How do I set up a time loop in Unity - c#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject enemy;
float defaultSpawnTime = 1f;
float spawnTime = 1f;
float realTime = 4f;
void Start()
{
}
void Update()
{
spawnTime-= Time.deltaTime;
realTime += Time.deltaTime;
if (spawnTime < 0 && realTime<30f && realTime>5f)
{
GameObject go = Instantiate(enemy, new Vector3(Random.Range(-2.3f, 2.3f), 8f, 0), Quaternion.Euler(0, 0, 0)) as GameObject;
GameObject go1 = Instantiate(enemy, new Vector3(Random.Range(-2.3f, 2.3f), 8f, 0), Quaternion.Euler(0, 0, 0)) as GameObject;
spawnTime = defaultSpawnTime;
if (realTime > 30f)
{
realTime = 0f;
}
}
}
}
My game is 2D.
In the game, a meteorite rains every 2 seconds from above.
The player struggles with these.
But since the game is very tiring, I do not want meteorites to rain for 5 seconds every 30 seconds, so the player will have the opportunity to rest.
I also wanted to create a time variable and increase it in real-time.
And I tried the meteor shower to happen, provided the time was between 5 and 30.
If the time is greater than 30, I wanted to return it to 0 and loop it, but after 30 it continues to progress.
I don't know where I went wrong, can you help me?

Just move the if statment outside of first if. Because after realtime > 30 you condition " && realTime<30f && " not fulfilled
void Update()
{
spawnTime-= Time.deltaTime;
realTime += Time.deltaTime;
if (spawnTime < 0 && realTime<30f && realTime>5f)
{
GameObject go = Instantiate(enemy, new Vector3(Random.Range(-2.3f, 2.3f), 8f, 0), Quaternion.Euler(0, 0, 0)) as GameObject;
GameObject go1 = Instantiate(enemy, new Vector3(Random.Range(-2.3f, 2.3f), 8f, 0), Quaternion.Euler(0, 0, 0)) as GameObject;
spawnTime = defaultSpawnTime;
}
if (realTime > 30f)
{
realTime = 0f;
}
}

It gets hard to manage time like that after you add few timers. Try using Coroutines like:
void Start(){
StartMeteorShower(5f);
}
void StartMeteorShower(float time){
StartCoroutine(StartMeteorShowerDelayed(time);
}
IEnumerator StartMeteorShowerDelayed(float delay){
yield return new WaitForSeconds(delay);
StartCoroutine(MeteorShowerCoroutine());
}
IEnumerator MeteorShowerCoroutine(){
var startTime = Time.time;
var delay = new WaitForSeconds(1f);
do
{
SpawnMeteor();
yield return delay;
}
while(Time.time - startTime < _meteorShowerDuration);
//here you can queue next meteor shower
StartMeteorShower(30f);
}

Related

Unity 2d : Objects being reinstantiated on scene reload after being destroyed

GOAL
So I'm creating a top down arena battler type game, and I want you to be able to restart the game by pressing R.
PROBLEM
When I press R, the whole scene resets as it should, except all the enemies that were previously instantiated (and then destroyed) are spawned again, all at once.
CODE
This is the enemy spawning code :
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawn : MonoBehaviour
{
private float nextActionTime = 0.0f;
public float period = 5f;
public GameObject enemy;
void Update()
{
if (Time.time > nextActionTime ) {
nextActionTime += period;
GameObject clone = Instantiate(enemy, new Vector3(-1, 3, 0), Quaternion.identity);
clone.tag = "enemy";
}
}
}
This is the player code, responsible for restarting the scene (I have marked what I belive to be the relevantant sections with dashes) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
public GameObject Shield;
public GameObject ShieldInstance;
public float moveSpeed = 4.3f;
public float sheildSpeed = 5f;
Vector2 movement;
AudioSource woop;
AudioSource waa;
----------------------------
GameObject[] enemies;
----------------------------
bool isDead = false;
void Start() {
woop = GameObject.Find("Main Camera/ShieldSFX").GetComponent<AudioSource>();
waa = GameObject.Find("Main Camera/DefeatSFX").GetComponent<AudioSource>();
}
void Update()
{
--------------------------------------------------------------
enemies = GameObject.FindGameObjectsWithTag("enemy");
--------------------------------------------------------------
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
Vector3 mouseScreen = Input.mousePosition;
Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);
transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 90);
if (Input.GetMouseButtonDown(0))
{
if (ShieldInstance != null || transform.GetChild(0).GetComponent<SpriteRenderer>().enabled == false) { return; }
woop.Play();
ShieldInstance = Instantiate(Shield, transform.position + transform.forward + transform.up, transform.rotation);
ShieldInstance.transform.parent = transform;
}
if (Input.GetMouseButtonUp(0))
{
if (ShieldInstance == null) { return; }
ShieldInstance.transform.parent = null;
ShieldInstance.GetComponent<ShieldController>().LaunchForward(sheildSpeed);
Destroy(ShieldInstance, 2.3f);
}
-------------------------------------------------------------------------------
if (Input.GetKey("r")) {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
foreach (GameObject one in enemies) {
Destroy(one);
}
}
-------------------------------------------------------------------------------
}
void FixedUpdate() {
if (!isDead) {
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.tag == "enemy") {
waa.Play();
GameObject.Find("Canvas/gameover").GetComponent<Text>().enabled = true;
transform.GetChild(0).GetComponent<SpriteRenderer>().enabled = false;
GetComponent<PolygonCollider2D>().enabled = false;
}
}
}
And this is the enemy code :
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyFollow : MonoBehaviour
{
public float moveSpeed;
public ParticleSystem explode;
AudioSource boom;
Vector2 movement;
GameObject player;
Rigidbody2D rb;
SpriteRenderer sr;
PolygonCollider2D pc;
void Start() {
rb = GetComponent<Rigidbody2D>();
sr = transform.GetChild(0).GetComponent<SpriteRenderer>();
pc = GetComponent<PolygonCollider2D>();
player = GameObject.Find("Player");
boom = GameObject.Find("Main Camera/ExplodeSFX").GetComponent<AudioSource>();
}
void Update()
{
Vector2 difference = (player.transform.position - new Vector3(2, .5f, 0)) - transform.position;
if (difference.x > 0) {
movement.x = 1;
} else if (difference.x < 0){
movement.x = -1;
} else {
movement.x = 0;
}
if (difference.y > 0) {
movement.y = 1;
} else if (difference.y < 0){
movement.y = -1;
} else {
movement.y = 0;
}
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.tag == "shield") {
StartCoroutine(ExplodeF());
}
}
private IEnumerator ExplodeF() {
explode.Play();
boom.Play();
sr.enabled = false;
pc.enabled = false;
yield return new WaitForSeconds(explode.main.startLifetime.constantMax);
Destroy(gameObject);
}
}
I would really appreciate any help!
If you want / need more details, just leave a comment :)
The problem is in Time.time, it is time since the start of the application, not since the start of the scene. So if you were in the game 30 secs, Time.time is 30 secs. If you reload the scene it is still 30 secs.
You have to count the time passed since entering the scene. Then it won't respawn all the enemies on scene reload.
When you restart the scene, everything is being destroyed and reset back except for the time.
The problem in your code is in the enemy spawner. Time.time returns the time passed since you started the game (and not since the scene was loaded). So, after the restart the if condition is true and the enemies are spawned.
If you want to count the time (in seconds) since the scene was loaded, what you can do is to add a variable in the enemy spawner class that would count the time
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawn : MonoBehaviour
{
private float nextActionTime = 0.0f;
private float timeSinceStart = 0;
public float period = 5f;
public GameObject enemy;
void Update()
{
if (timeSinceStart > nextActionTime ) {
nextActionTime += period;
GameObject clone = Instantiate(enemy, new Vector3(-1, 3, 0), Quaternion.identity);
clone.tag = "enemy";
}
timeSinceStart += Time.deltaTime;
}
}
See Time.deltaTime

Can not play disabled audio source (Unity 2d)

I have a little problem; I'm trying to develop my own endless jumping game. That's why I created an enemy type that can fire small projectiles. Every time it shoots, a sound should be played, but it won't. Debug: "Can not play disabled audio source."
Originally that only didn't work out for the prefabs, but recently it didn't work out for the first, original opponent either. (I kinda messd up spmething in the code)
Every proposed solution would help me a lot :)
I know, there are some posts about this topic out there, but none of this has helped yet...
The kinda messy code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyingEnemy : MonoBehaviour {
public float startTimeBtwShots;
private float timeBtwShots;
private Vector3 spawnPoint;
public AudioSource shot;
public Animator animator;
private float flyingSpeed;
private float turningPoint;
private bool moving;
public GameObject projectile;
public GameObject player;
void Start ()
{
turningPoint = Mathf.Abs(transform.position.x);
flyingSpeed = 0.008f;
timeBtwShots = startTimeBtwShots;
moving = true;
shot.enabled = true;
}
void Update ()
{
//Ignore, moves enemy from left to right and back
if (moving)
{
transform.position = new Vector3(transform.position.x + flyingSpeed, transform.position.y, transform.position.z);
}
if (transform.position.x >= turningPoint || transform.position.x <= turningPoint * -1)
{
flyingSpeed *= -1;
transform.position = new Vector3(transform.position.x + flyingSpeed, transform.position.y, transform.position.z);
if (flyingSpeed <= 0) transform.eulerAngles = new Vector3(0, 180, 0);
else if (flyingSpeed >= 0) transform.eulerAngles = new Vector3(0, 0, 0);
}
//Important part: projectile gets spawn and a bit earlier the sound should be played...
if (timeBtwShots <= 0)
{
spawnPoint = transform.position;
spawnPoint.y -= 0.35f;
Instantiate(projectile, spawnPoint, Quaternion.identity);
timeBtwShots = startTimeBtwShots;
animator.SetBool("Shooting", false);
}
else
{
timeBtwShots -= Time.deltaTime;
if (timeBtwShots <= 0.3)
{
shot.volume = 1;
shot.Play();
}
if (timeBtwShots <= 0.6)
{
animator.SetBool("Shooting", true);
}
}
if (player.transform.position.y >= transform.position.y + 5.5f)
{
Destroy(gameObject);
}
}
}
Thank you in advance!!
Ego | Jakob
You should try on your Start function: shot.gameObject.SetActive(true);
It can be the problem.

Scale GameObject over time

I made a test game in unity that makes it so when I click on a button, it spawns a cylinder created from a factory class. I'm trying to make it so when I create the cylinder, its height shrinks over the next 20 seconds. Some methods I found are difficult to translate into what I'm doing. If you could lead me to the right direction, I'd very much appreciate it.
Here's my code for the cylinder class
public class Cylinder : Shape
{
public Cylinder()
{
GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
cylinder.transform.position = new Vector3(3, 0, 0);
cylinder.transform.localScale = new Vector3(1.0f, Random.Range(1, 2)-1*Time.deltaTime, 1.0f);
cylinder.GetComponent<MeshRenderer>().material.color = Random.ColorHSV();
Destroy(cylinder, 30.0f);
}
}
This can be done with the Time.deltaTime and Vector3.Lerp in a coroutine function. Similar to Rotate GameObject over time and Move GameObject over time questions. Modified it a little bit to do just this.
bool isScaling = false;
IEnumerator scaleOverTime(Transform objectToScale, Vector3 toScale, float duration)
{
//Make sure there is only one instance of this function running
if (isScaling)
{
yield break; ///exit if this is still running
}
isScaling = true;
float counter = 0;
//Get the current scale of the object to be moved
Vector3 startScaleSize = objectToScale.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
objectToScale.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
yield return null;
}
isScaling = false;
}
USAGE:
Will scale GameObject within 20 seconds:
StartCoroutine(scaleOverTime(cylinder.transform, new Vector3(0, 0, 90), 20f));
Check out Lerp. A general example of how to use it would be something like this:
float t = 0;
Update()
{
t += Time.deltaTime;
cylinder.localScale = new Vector3(1, Mathf.Lerp(2f, 1f, t/3f), 1); // shrink from 2 to 1 over 3 seconds;
}
You will create a new monobehaviour script and add it to your primitive. Then you wil use "Update" method of monobehaviour (or use coroutine) for change object over time.
Monobehaviour must be look like this:
public class ShrinkBehaviour : MonoBehaviour
{
bool isNeedToShrink;
Config currentConfig;
float startTime;
float totalDistance;
public void StartShrink(Config config)
{
startTime = Time.time;
currentConfig = config;
totalDistance = Vector3.Distance(currentConfig.startSize, currentConfig.destinationSize);
isNeedToShrink = true;
transform.localScale = config.startSize;
}
private void Update()
{
if (isNeedToShrink)
{
var nextSize = GetNextSize(currentConfig);
if (Vector3.Distance(nextSize, currentConfig.destinationSize) <= 0.05f)
{
isNeedToShrink = false;
return;
}
transform.localScale = nextSize;
}
}
Vector3 GetNextSize(Config config)
{
float timeCovered = (Time.time - startTime) / config.duration;
var result = Vector3.Lerp(config.startSize, config.destinationSize, timeCovered);
return result;
}
public struct Config
{
public float duration;
public Vector3 startSize;
public Vector3 destinationSize;
}
}
For using this, you must do next:
public Cylinder()
{
GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
var shrink = cylinder.AddComponent<ShrinkBehaviour>();
shrink.StartShrink(new ShrinkBehaviour.Config() { startSize = Vector3.one * 10, destinationSize = Vector3.one * 1, duration = 20f });
cylinder.transform.position = new Vector3(3, 0, 0);
cylinder.GetComponent<MeshRenderer>().material.color = Random.ColorHSV();
Destroy(cylinder, 30.0f);
}
You must remember, monobehaviour-script must be in separate file, and must have name similar to monobehaviour-class name. For example, ShrinkBehaviour.cs;

Unity3D Rigidbody player jump is limited while running

I'm trying to programming movements of a main FPS character with a Rigidbody.
Camera and ZQSD displacements works well but jumping while moving is being very restricted :
Immobile jumping : Y (min) = 1; Y (max) = 2.7;
Mobile jumping : Y (min) = 1; Y (max) = 1.9;
It's very frustrating, especially for a platform game. Personnaly, I'd like to get same result for both actions.
Here's my C# code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPSMovement : MonoBehaviour {
public float walkAcceleration = 150f;
public float maxWalkSpeed = 10f;
public float jumpVelocity = 500f;
public float maxSlope = 45f;
private Rigidbody rb;
private Vector2 horizontalMovement;
private bool isGrounded = false;
private bool doubleJumped = false;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if(rb.velocity.magnitude > maxWalkSpeed){
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxWalkSpeed);
}
transform.rotation = Quaternion.Euler (0, GetComponentInChildren<FPSCamera>().currentYRotation, 0);
// Entrées clavier ZQSD déplaçant le joueur
float x = 0, y = 0, z = 0;
if (Input.GetKey (KeyCode.Z)) {
x += 1f;
}
if (Input.GetKey (KeyCode.S)) {
x -= 1f;
}
if (Input.GetKey (KeyCode.Q)) {
z -= 1f;
}
if (Input.GetKey (KeyCode.D)) {
z += 1f;
}
// Arrêt prompt du glissement
if ((!Input.GetKey (KeyCode.Z) && !Input.GetKey (KeyCode.S) && !Input.GetKey (KeyCode.Q) && !Input.GetKey (KeyCode.D))
&& isGrounded) {
rb.velocity /= 1.1f;
}
// Saut du joueur
if (Input.GetKey (KeyCode.Space) && isGrounded) {
rb.AddForce (0, jumpVelocity, 0);
// Deuxième saut
}
if (Input.GetKey (KeyCode.Space) && !doubleJumped) {
rb.AddForce (0, jumpVelocity, 0);
doubleJumped = true;
}
rb.AddRelativeForce (z * walkAcceleration, 0, x * walkAcceleration);
}
void OnCollisionStay(Collision other) {
foreach (ContactPoint contact in other.contacts) {
if (Vector3.Angle (contact.normal, Vector3.up) < maxSlope) {
isGrounded = true;
doubleJumped = false;
}
}
}
void OnCollisionExit() {
isGrounded = false;
}
}
I also have impression that player hovers when moving in the airs, but this is not the main question and above all just an impression.
A simple issue with few words to explain, but a real problem to resolve for this type of gaming !
May I ask you, Stackoverflow people to gently help me, a thousand thanks by advance mates.
PS: My native language is the french, so please, don't lose too much time on grammar or other english subtlety :)
It is because you clamp the total velocity of the player where you should only clamp the horizontal speed. If you walk at maximum speed and jump, your jump speed is added to the total velocity and your character should actually be travelling at a higher speed than the maximum walking speed. To fix it, you do this:
// Isolate the horizontal component
Vector3 horizontalVelocity = rb.velocity;
horizontalVelocity.y = 0;
if (horizontalVelocity.magnitude > maxWalkSpeed) {
// Clamp the horizontal component
Vector3 newVelocity = Vector3.ClampMagnitude(horizontalVelocity, maxWalkSpeed);
// Keep the original vertical velocity (jump speed)
newVelocity.y = rb.velocity.y;
rb.velocity = newVelocity;
}

Unity C# yield return new WaitForSeconds is stopping coroutine

I wanted to use a coroutine to make the player wait a little before shooting a new bullet but it never gets past the yield. Here's the code
protected override void Start () {
base.Start ();
animator = GetComponent<Animator> ();
score = GameManager.instance.playerScore;
playerLives = GameManager.instance.playerLife;
}
void Update(){
int horizontal = (int)Input.GetAxisRaw("Horizontal");
AttemptMove (horizontal, 0);
if (Input.GetKeyDown ("space")) {
if(canShoot){
Vector3 shootPosition = transform.position + new Vector3 (0, 0.5f, 0);
Instantiate (bullet, shootPosition, Quaternion.identity);
StartCoroutine(Shoot());
}
}
}
IEnumerator Shoot(){
Debug.Log("Start");
canShoot=false;
yield return new WaitForSeconds (shootingTimeDelay);
canShoot=true;
Debug.Log("End");
}
shootingTimeDelay is set to 1.1f. I am not destroying my gameObject anywhere and it works properly in other scripts in my project.
It never prints End. I don't get what is wrong
I would say don't use a coroutine for something like this.
Trying doing this and see if you get better restults
private float time = 0;
public float fireTime = 1.1f;
private void Update()
{
time += Time.deltaTime;
if(Input.GetKeyDown("space") && time >= fireTime)
{
Vector3 shootPosition = transform.position + new Vector3 (0, 0.5f, 0);
Instantiate (bullet, shootPosition, Quaternion.identity);
time = 0;
}
}
Ok i found the bug. I had a method in its superclass that called StopAllCoroutines but for some reason i never tought of it until now. Changed that to StopCoroutine("name of my coroutine") and now it works perfect :)

Categories

Resources