I am trying to play my animator's animation 5 times. That is after every animation that ends, it has to replay it for another time. How do I do this?
public Animator anim;
void Start ()
{
StartCoroutine(PlayAnimInterval(5));
}
private IEnumerator PlayAnimInterval(int n)
{
while (n > 0)
{
anim.Play("wave", -1, 0F);
--n;
//yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length); //Returns 1 which is wrong
}
}
Use the for loop in IEnumerator to solve the problem. Also make sure you enter the layer number correctly. Here the wave state is repeated 5 times in layer zero.
private IEnumerator PlayAnimInterval(int n)
{
for (int i = 0; i < n; i++)
{
anim.Play("wave", 0, 1);
yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length);
}
}
How to detect Animator Layer?
The animator layer is an array. By adding each layer in the layers section, its index is also added. You can find its code below.
Repeat State Info with Behaviour
In this method you can solve the problem of repetition in any state. Just create a behavior like the one below and just add the number of repetitions. This method also works independently of the layer.
public class Repeat : StateMachineBehaviour
{
public int repeatTime;
public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (repeatTime <= 0) return;
repeatTime--;
animator.Play(stateInfo.fullPathHash);
}
}
Related
Context
Hello, currently creating a clone of "Crossy Road" and what I'm trying to do is to spawn my moving object called "Vehicle" at a random speed and rate of spawn. This is also applicable to "Plank", but I will start first with the vehicle. So far, everything is working fine as intended for the game mechanics, but I would like to finalize with this issue so it is fully functional in terms of playability.
Problem
My issue now is I 3 different spawns objects: grass, river, and road. Each object holds other objects (let's call it spawners) depending of what field is being spawn. For example, if grass field object is spawned, it will spawn trees depending in a random varied selection. Another example is with road field. When the road is spawned, a vehicle will be spawned from either left or right in its current initial position. This vehicle will moves as intended with a random speed, but not with the original spawn position and rate (as shown in the GIF. The vehicle spawns in the middle of the road and not in the beginning of the left/right road).
As far I'm aware, my rate is currently unused because it is not the main issue I want to solve. However, the issue now is with the transform position not working as I have pictured in my head. So what is happening is that when the road is spawned again, the vehicle is spawned in the middle of the trajectory instead of resetting to the beginning.
Also, I have noticed that when I print the vehicle object, the Z-axis has a weird number compared to the original position.
Attempts done
I have been thinking that maybe it is the way I have set everything up. I have 4 vehicle objects with a child object called "Tank". However, in each vehicle object, I'm using SetActive(...) only and not really reusing the object itself to the beginning. Later on, I want to organize this spaghetti code and optimize it (e.g ObjectPool to spawn my roads and other GameObjects after hitting a certain range, adding a player range detection to spawn a field to name a few).
To be honest, my whole code feels bloated for something simple. This will be fixed once everything is working accordingly.
Code (DISCLAIMER: there is the possibility that there are unused variables)
SpawnManager.cs (some links provided too from learning to make this)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
** Weighted randomness: https://forum.unity.com/threads/random-numbers-with-a-weighted-chance.442190/
** Scriptable Object Weight spawn example: https://www.youtube.com/watch?v=FCksj9ofUgI&ab_channel=LlamAcademy
** From scratch loot tables with Scriptable Objects to make a loot table: https://www.youtube.com/watch?v=tX3RWsVLnzM&ab_channel=GregDevStuff
** Creating a random with an animation curve: https://www.youtube.com/watch?v=zw1OERK5xvU&ab_channel=HamzaHerbou
** Random Vehicle position spawn (maybe this can help me): https://stackoverflow.com/questions/51312481/move-spawn-object-to-random-position
*/
public class SpawnManager : MonoBehaviour
{
public GameObject Player;
public Spawn[] Field;
public GameObject[] SpawnObjectTrees;
public GameObject[] SpawnObjectVehicles; //different vehicles
public GameObject[] SpawnObjectPlanks; //3 sizes (small, medium, large)
private PlayerControl2 playerControlScript;
private int distancePlayer;
private int toggle;
private bool keepSpawning;
bool vehicleFlag = false;
bool plankFlag = false;
public float randomNumSpawn;
void Awake()
{
keepSpawning = true;
playerControlScript = GameObject.Find("PlayerObject").GetComponent<PlayerControl2>();
InvokeRepeating("Spawner", 3f, randomNumSpawn);
}
void Update()
{
if (Input.GetButtonDown("up") && !playerControlScript.gameOver)
SpawnField();
}
void Spawner()
{
bool activeLeft = false;
bool activeRight = false;
if (vehicleFlag)
{
print(initialObjectSpawn);
for (int i = 0; i < SpawnObjectVehicles.Length; i++)
{
print($"{SpawnObjectVehicles[i]}: {SpawnObjectVehicles[i].transform.position}"); //Here I get the weird position.z values pretty wonky
toggle = Random.Range(0, 2);
if (toggle == 1 && !activeLeft)
{
activeLeft = true;
SpawnObjectVehicles[i].SetActive(true);
}
if (toggle == 0 && !activeRight)
{
activeRight = true;
SpawnObjectVehicles[i].SetActive(true);
}
else
SpawnObjectVehicles[i].SetActive(false);
}
}
}
void SpawnField()
{
//I want to spawn the vehicles, planks, and trees in sets accordingly to the field (grass, river, road)
//For vehicles and planks, they can move horizontally from either -z or z boundaries
//NOTE: keepSpawning may be useless if i have a playerControlScript.gameOver already in here
if (keepSpawning)
{
distancePlayer += 3;
Vector3 intPos = new Vector3(0, 0, 0);
int i = Random.Range(0, 1000);
for (int j = 0; j < Field.Length; j++)
{
if (i >= Field[j].minProbabilityRange && i <= Field[j].maxProbabilityRange)
{
intPos = new Vector3(distancePlayer, -1f, 0);
GameObject Surface = Instantiate(Field[j].spawnField);
if (Surface.CompareTag("Grass"))
TreeToggle();
if (Surface.CompareTag("Road"))
{
vehicleFlag = true;
VehicleToggle();
}
// if (Surface.CompareTag("River")) this will be the same as vehicle
// {
// plankFlag = true;
// PlankToggle();
// }
//Add spawn for vehicles and planks with given spawnrate/spawn intervals
Surface.transform.position = intPos;
vehicleFlag = false;
plankFlag = false;
}
}
}
}
void TreeToggle()
{
int counter = 0;
for (int i = 0; i < SpawnObjectTrees.Length; i++)
{
int toggle = Random.Range(0, 2); //[0, 2)
if (toggle == 1 && counter < 5) //True and when there are already 5-4 trees to toggle
{
counter++;
SpawnObjectTrees[i].SetActive(true);
}
else //fills the rest to inactive Trees
SpawnObjectTrees[i].SetActive(false);
}
}
void VehicleToggle()
{
// I have Left and Right with 2 vehicles in each. My goal is to setActive one of them each side at a time with a different interval spawnrate and speed
Spawner();
}
void PlankToggle()
{
Spawner();
}
}
[System.Serializable]
public class Spawn
{
public GameObject spawnField;
public float minProbabilityRange = 0.0f;
public float maxProbabilityRange = 0.0f;
}
Hierarchy/Inspector
If there is any information you want to know, feel free to ask and I will make a quick edit to fulfill these goals. Again, thank you for your time and appreciate it :D I hope you are having a good day!
I have game for multiple players where each user selects their hero before game starts and that loads the selected heroes into the battle arena.
I have small issue with getting the instantiation to spawn in correct numbers of players
The method that I have for Spawning the characters:
private void Placement()
{
for (int i = 0; i < SelectedCards.Count; i++)
{
for (int t = 0; t < AvailableHeroes.Count; t++)
{
if (AvailableHeroes[t].name == SelectedCards[i].name)
{
Debug.Log(AvailableHeroes[t]);
// Instantiate(AvailableHeroes[t], PlayerSpawnLocation[t].transform.position, transform.rotation);
}
{
}
}
}
}
This script checks for amount of selected hero cards and puts it against my list that has all the available heroes to choose from(prefabs).
The debug.log shows that only the correct heroes get called.
Instantiate ends up spawning a loot of heroes instead of the selected amount.
For clarity I attach full class:
{
private int playerSize; //amount of choices for card selection
private GameManager GM;
[Header("Lists for Spawning in Heroes")]
public List<GameObject> SelectedCards;
public List<GameObject> AvailableHeroes;
public List<Transform> PlayerSpawnLocation;
[Header("Canvas used for the game")]
public Transform GameCanvas;
public Transform CharacterCanvas;
//When scene starts it takes how many players will be picking a card.
void Start()
{
//connects this script with gamenmanager to be able to manipulate the cameras
GM = GameObject.Find("GameManager").GetComponent<GameManager>();
//gets playersize information from main menu selection
PlayerPrefs.GetInt("PlayerSize");
playerSize = PlayerPrefs.GetInt("PlayerSize");
SelectedCards = new List<GameObject>();
//enables/disables correct canvas not to cause any problems when we initiate this scene
GameCanvas.gameObject.SetActive(false);
CharacterCanvas.gameObject.SetActive(true);
}
// Update is called once per frame
void Update()
{
if (playerSize <= 0)
{
Placement();
GM.CharacterSelectionCamera.enabled = false;
GameCanvas.gameObject.SetActive(true);
CharacterCanvas.gameObject.SetActive(false);
GM.BattleCamera.enabled = true;
}
}
public void PlayerSelected(int cardPicked)
{
playerSize -= cardPicked;
}
private void Placement()
{
for (int i = 0; i < SelectedCards.Count; i++)
{
for (int t = 0; t < AvailableHeroes.Count; t++)
{
if (AvailableHeroes[t].name == SelectedCards[i].name)
{
Debug.Log(AvailableHeroes[t]);
// Instantiate(AvailableHeroes[t], PlayerSpawnLocation[t].transform.position, transform.rotation);
}
{
}
}
}
}
}
I hope someone can explain where I am going wrong with this.
Thanks,
I got the answer, I guess I was just being tired from working and could not see the obvious.
For those who wonder what solution is
The method gets called each frame thus it continues to endlessly spawn objects
There are 2 ways to fix it
1 Make coroutine and then return after you make your initial batch
2 Use a boolean at update so not only it checks player size but also whenever it can spawn it or not, you set the boolean to false after method get called.
I did not even notice the update function part.
Just a heads up, in your start function, PlayerPrefs.GetInt("PlayerSize"); is not doing anything since the value is not saved anywhere.
Good morning Developers I know that this question has been asked before but I didn't find a solution to my problem in those answers, first I'd like to tell you that I'm just a beginner in unity :p , well here is my problem : I'm building a brick breaker game what I'm trying to do is to change the sprite of a brick when it gets hit by the ball, to do so I'm using this script :
public int maxHits;
public int timesHit;
private LevelManager levelManager;
public Sprite[] hitSprites;
void Start () {
timesHit = 0;
levelManager = GameObject.FindObjectOfType<LevelManager> ();
}
void OnCollisionEnter2D(Collision2D collision) {
print ("collison");
timesHit++;
}
// Update is called once per frame
void Update () {
if (timesHit >= maxHits) {
Destroy (gameObject);
} else {
LoadSprite ();
}
}
void LoadSprite(){
int spriteIndex = timesHit - 1;
this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
}
}
but I'm getting this error : IndexOutOfRangeException: Array index is out of range. Brick.LoadSprite () (at Assets/Scripts/Brick.cs:34)
and I'm getting it every frame!, so it's slowing the game scene too much and I can't test my game anymore. Can you please tell me what did i do wrong and how to fix it?It may help if you direct me to a course to learn more about my error and not't do it again.
This is happening because your times hit starts at 0, then when you load a sprite you deduct 1 giving you -1. In the following line of LoadSprite you then attempt to access your hitSprites array at index -1 (hitSprites[-1]) which of course, is out of bounds.
At the very least, I would add some validation to check the bounds of your index.
You need to clamp spriteIndex not to go less then 0 (first array index) and over hitSprites.Length reduced by 1 (last array index).
void LoadSprite(){
int spriteIndex = Mathf.Clamp(timesHit - 1, 0, hitSprites.Length - 1);
this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
}
}
Tip:
dont call too many GetComponent in Update.. Prefetch all components in awake for later use.
private SpriteRenderer _spriteRenderer;
void Awake()
{
_spriteRenderer = GetComponent<SpriteRenderer> ();
}
I can't tell tell what you are doing but you are getting that error because you are accessing index that is >= hitSprites length.
You should check if spriteIndex variable is less than hitSprites length before using it.
Your new LoadSprite function should be:
void LoadSprite()
{
int spriteIndex = 0;
//Don't decrement if timesHit is 0
if (timesHit > 0)
{
spriteIndex = timesHit - 1;
}
else
{
spriteIndex = timesHit;
}
//Return/Exit function if spriteIndex is equals or more than hitSprites length
if (spriteIndex > hitSprites.Length - 1)
{
return;
}
this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
}
#Programmer #Dan-Cook #user2867426
fist, i'd like to thank you all for your answers, i learned so much from you guys so, big thank you!!
well i kinda by mistake fixed my error lol.
when i was optimising my code i noticed that i was using this if condition in the update() while i could use it directly to OnCollisionEnter2D():
if (timesHit >= maxHits) {
Destroy (gameObject);
} else {
LoadSprite ();
}
and somehow that fixed my problem i don't know how,
maybe the problem was that it was called every single frame and now it's not!
if you guys could explain to me how moving that if condition from update() fixed the problem i really appreciate it ^^
thank you all ones again.
I'm trying to implement a damage over time system, but Unity keeps saying "Trying to Invoke method...Couldn't be Called." The method I want to call uses the parameters "Collider coll", but from my research you can't invoke if the method has said paremters.
Here is my code:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class DamageOverTime : MonoBehaviour
{
public int PHP; //PHP = Player Health from PlayerHealth.cs script.
public int Damage; //Amount of damage.
public int DamageOverTime; //Damage over time.
public float DamageInterval_DOT = .25f; //Damage interval for damage over time.
public string Level;
PlayerHealth player;
void Start()
{
player = GameObject.Find("Player").GetComponent<PlayerHealth>();
InvokeRepeating("OnTriggerEnter", DamageInterval_DOT, DamageInterval_DOT);
}
void Update()
{
PHP = GameObject.Find("Player").GetComponent<PlayerHealth>().PlayerHP;
if (PHP <= 0)
{
SceneManager.LoadScene(Level);
}
}
void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.tag == "Player")
{
GameObject.Find("Player").GetComponent<PlayerHealth>().PlayerHP = PHP - Damage;
}
if (coll.gameObject.tag == "Ball")
{
gameObject.SetActive(false);
SceneManager.LoadScene(Level);
}
}
}
My goal is to get the OnTriggerEnter function to loop ever 1/4 of a second (or lower possibly). Current upon entering a collider my health is drained by 60% in about a second which is far too fast. How should I work around this?
You can't use InvokeRepeating with OnTriggerEnter, because it's a trigger, which means it will trigger once when entrance of its holder occured.
Also InvokeRepeating means that you want to keep repeating an action continously which is not the case here. You want your trigger to occur once and then remove health points over time.
Solution - Coroutine
Unity3D makes custom usage of IEnumerable and yield keyword called Coroutine that always returns an IEnumerator. How it works? It will return control on every yield there is in our Coroutine and then will go back to exact point where it gave back control instead of starting function execution from scratch.
Code:
void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.tag == "Player")
{
StartCoroutine("DamageOverTimeCoroutine");
}
if (coll.gameObject.tag == "Ball")
{
gameObject.SetActive(false);
SceneManager.LoadScene(Level);
}
}
public IEnumerator DamageOverTimeCoroutine()
{
var dotHits = 0;
while (dotHits < 4)
{
//Will remove 1/4 of Damage per tick
GameObject.Find("Player").GetComponent<PlayerHealth>().PlayerHP -= Damage / 4;
dotHits++;
//Will return control over here
yield return new WaitForSeconds(DamageInterval_DOT);
//And then control is returned back here once 0.25s passes
}
}
There's of course room for improvement in this Coroutine. You can pass parameters to it, same as you can to any other method in C#. Also you can implement other invervals that are not hardcoded. Code above is just a simple example on how to deal with such scenarios.
For continous damage over time
public IEnumerator DamageOverTimeCoroutine()
{
var dotHits = 0;
var player = GameObject.Find("Player").GetComponent<PlayerHealth>();
while (true)
{
//Stop removing damage, player is dead already
if (player.PlayerHP <= 0)
yield break;
//Will remove 5 Damage per tick
player.PlayerHP -= 5;
dotHits++;
//Will return control over here
yield return new WaitForSeconds(DamageInterval_DOT);
//And then control is returned back here once 0.25s passes
}
}
To stop Coroutine somewhere else from code use StopCoroutine("DamageOverTimeCoroutine") to stop certain coroutine type or StopAllCoroutines() to stop all coroutines that are active now.
I'm trying to make a GameObject appear and disappear for a finite amount of time (Lets put the time function aside for now).
Here's what I came out with:
using UnityEngine;
using System.Collections;
public class Enemy1Behavior : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
this.gameObject.SetActive(false); // Making enemy 1 invisible
Debug.Log("Update called");
DisappearanceLogic(gameObject);
}
private static void DisappearanceLogic(GameObject gameObject)
{
int num = 0;
while (num >= 0)
{
if (num % 2 == 0)
{
gameObject.SetActive(false);
}
else
{
gameObject.SetActive(true);
}
num++;
}
}
}
Now when I click the play button in Unity the program just don't respond, and I can only quit it from the task manager using End Task.
(And yes I know there a infinite loop in the method).
So I guess Im doing something wrong. What is the best way for making a Gameobject Blink/Flash/appear-disappear in Unity?
Thanks guys.
You are using an infinite loop which locks your Update() completely, because num will always be greater then 0.
So you could use InvokeRepeating (http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html)
public GameObject gameobj;
void Start()
{
InvokeRepeating("DisappearanceLogic", 0, interval);
}
void DisappearanceLogic()
{
if(gameobj.activeSelf)
{
gameobj.SetActive(false);
}
else
{
gameobj.SetActive(true);
}
}
interval is a float - something like 1f 0.5f etc.
You can make animation for blinking etc - Animations in Mecanim. Appearing and disappearing you can achieve using gameObject.SetActive(true/false);. If you want to make something with time its better to use Coroutines or just Invoke with delay parameter - Invoke Unity Docs.