Call upon multiple animation events on player - c#

If I have a player and let's say a red ball hits my player and the player goes on fire... OK easy enough I did that. But now I want to add to the code below under the if statement that a blue ball hits my player and freezes or a black ball and causes death animation... But it's just not working out to add multiple animation events to 1 player.
Public GameObject GoneGo;
void OnTriggerEnter2D(Collider2D collisionObject)
{
if (collisionObject.gameObject.tag == "Orb")
{
PlayGone();
}
}
void PlayGone()
{
GameObject gone = (GameObject)Instantiate(GoneGo);
gone.transform.position = transform.position;
}

What you could do is perform a name check on the collider that impacts the player. You already have it checked the tag, have it check for redball,blueball, or blackball as the GameObject name afterwards.
You can handle multiple animations by looking into unitys mechanim system, which is the animator component (not animation component).
https://community.mixamo.com/hc/en-us/articles/203879268-Unity-Mecanim-Animation-Basics
What you would do after setting up the animator is have it SetTrigger for different trigger parameters. Or, you could use something like SetBool with a bool in the parameters like HitByBlueBall or HitByRedBall.
Hope this helps!

Related

How do I add multiple animators to my character

I have my main character and added a capsule collider 2d so he would take damage when attacked. When the character is attacking, for example his body is out of the collider space and won’t take damage when hit. Please may you help me solve this issue, I’m very new to unity
One way of doing it is having two different hitboxes that does the same thing but with different shapes so they match two different states of your character.
Enable and disable them in a script when calling the attack using:
Public Collider2D attackCollider; //Drag your attack collider here in the inspector
Public Collider2D idleCollider; //Drag your idle collider here in the inspector
void AttackCollider() //call this fucntion to change to attack collider
{
attackCollider.enabled == true;
idleCollider.enabled == false;
}
void IdleCollider() //call this function to go back to normal collider.
{
attackCollider.enabled == false;
idleCollider.enabled == true;
}
If you want to I'm sure you can incorporate these two functions into an already existing attack function if you have one.
In 3D, you could simply make the Collider a child of any of your bones, it would get animated altogether.
But you mentioned Collider2D, so:
If you have a 2D Rig with bones, you can do the same as obove
If you have a Keyframe Animation, you could animate the collider to match your animation

Unity: Enemy objects passing through box collider on edges of background

I'm developing a simple 2D Unity game (which I'm very new to so sorry if this is a silly question!) in which my player can eat its enemies by colliding with them. This works fine as I'm just selecting the "is trigger" component for the enemies and using this code in my Player class:
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Hit detected");
Destroy(other.gameObject);
transform.localScale += new Vector3(x, y, z);
}
However, this means the colliders placed around the border of my background image aren't stopping the enemies. What's the best fix for this?
I don't understand very well your question. However it seems that your collisions are not working. So, remember that for have collisions actually taking place in your game you need to use colliders and that one of the two elements participating in the collision need to have the rigidbody component.
That will make the physics work in the engine, which triggers dont.
To check if that works you can debug with:
// called on collision
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("OnCollisionEnter2D");
}
From what I understood, you want only to detect triggers between the player and the enemies, but you still want these to collide with physic objects in your scene, such as background colliders.
One possible way to achieve this is to create a child object for the player object with a collider component with the trigger option set, and attaching a script to it to handle the triggers. Then, with the use of layers to group your player and enemy objects, you can uncheck the collision between them following: Edit -> Project Settings -> Physics 2D: "Layer Collision Matrix".
You can assign a script to any enemy, checking the distance with the player in each frame. Then you can Uncheck "is trigger"
Vector2.Distance

How can I make a particle remove an object?

In my game a player plants a bomb, the bomb explodes creating a fire effect, I would like my fire to be able to kill the player(blue cylinder) and any boxes it collides with. My box and player have colliders. My fire effect is instantiated when the bomb explodes.
How can I make my fire effect destroy my player and box objects?
Can I say something like if fire collider hits player collider, destroy player?
My code for the bomb is as follows
Instantiate(Firebolt, bomb.gameObject.transform.position, Quaternion.identity);
Game Layout
You can add a collider to Firebolt as well and use OnCollisionEnter on it, checking if the object you hit is a player or a box.
This is a simple example that destroys any player or box it collides with:
void OnCollisionEnter(Collision collision) {
GameObject other = collision.gameObject;
// Here I'm using tag to detect if the hit object is a player or a box
// but you can use name or other methods
if (other.tag == 'Player' || other.tag == 'Box') {
Destroy(other);
}
}
check out https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html
you can apply it to the ParticleSystem or to the target GOs that should react to it.
(u also need to activate collision detection on Particle System AND set it to world.

How to destroy child object of other player in UNET?

Right now, I have a multiplayer game where the player fires projectiles at other players that have a certain amount of bubbles. Once a player has lost all of their bubbles, they lose.
The issue I had previously was that on one screen, a first player could shoot out a second player's bubbles, but not all of the bubbles on the second player's screen would be popped, so I am trying to sync it over the network somehow.
The issue I'm seeing is that NetworkServer.Destroy requires finding the GameObject you intend to destroy by its NetworkIdentity, but only the root Player GameObject is allowed to have the NetworkIdentity component on it. What's the best way I can sync the destruction of a child object of a player?
The https://unity3d.com/fr/learn/tutorials/topics/multiplayer-networking/ course is very similar to the game you describe. It has a Bullet script attached to the fired bullets. If the bullets touched something, it check if it's a player, and apply damage to it (the player prefab has a Health script attached to it).
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
void OnCollisionEnter(Collision collision)
{
var hit = collision.gameObject;
/* Code below is from the original example
var health = hit.GetComponent<Health>();
if (health != null)
{
health.TakeDamage(10);
}*/
var bubble = hit.GetComponent<BubbleBehavior>();
if (bubble != null)
{
bubble.Popped(); // Implement a Popped function doing the magic
}
Destroy(gameObject);
}
}
In your case you can do the same, but instead of checking if the touched object is a player, simply check if it is a bubble, and do whatever you want from the touched bubble.
In the original example, they sync the health (it is a "SyncVar"), in your case you can sync the number of bubbles remaining.

Checking Animator on Unity script only work on first Object

Let me explain the question first :)
I have a hero and an enemy. It is a fighting game. Both hero and enemy have idle, block, punch and getHit animations and states in the Animation Controller. The hero and enemy have scripts attached to them. The hero is controlled by player and then enemy is controller by AI.
Now I attach the script to hero first and then the enemy. Now when the enemy punches and if hero is not defending the hero takes a hit. But if the enemy is not blocking and the hero hits the enemy doesnt take the hit. This is because the script was attached to hero first.
Now if I remove the script from both and attach the enemy script first to enemy and then attach the hero script to hero. The wiseversa is true. In the enemy the hits will be registered and on the hero the hits wont be registered.
I am not sure why this is happening and I have tried different things and still the problem persists.
I also tried looking everywhere online for solution but none of them addressed my concern.
Thanks :)
below is the enemy script that checks is enemy can take hit
void Update () {
myTick++;
currentTick = myTick;
GameObject player = GameObject.Find("theDude");
Animator pAnim = player.GetComponent<Animator>();
//Getting Hit
if (pAnim.GetBool("tIsPunching"))
{
if (anim.GetBool("bEnemyIsDefending") == false)
{
Debug.Log("enemy got hit");
anim.SetTrigger("tEnemyGotHit");
anim.SetBool("bEnemyIsDefending", true);
}
}
}
And here is the hero script that checks if hero can take hit.
void Update () {
totalTime += Time.deltaTime;
GameObject enemy = GameObject.Find("Enemy");
Animator eAnim = enemy.GetComponent<Animator>();
//Getting Hit
if (eAnim.GetBool("tEnemyIsPunching"))
{
if (anim.GetBool("bIsDefending") == false)
{
Debug.Log("player got hit");
anim.SetTrigger("tGotHit");
}
}
}
Instead of get object I used to have a public GameObject and attached the hero and enemy in the respective classes. But It doesnt make any difference.
I think your problem lies in your use of the triggers tEnemyIsPunching and tIsPunching. Triggers get reset whenever they cause a transition to occur
(see: https://docs.unity3d.com/Manual/AnimationParameters.html).
In your case tIsEnemyPunching or (tIsPunching) is getting reset in the same frame as it gets set. Here is an example of what one update loop may look like in your game if the hero script is added first:
Hero Update()
Check if enemy is punching
He is not, so don't do anything
Enemy Update()
Punch! Set 'bIsEnemyPunching' = true
Animation Update()
bIsEnemyPunching is true so transition to the punching animation
reset bIsEnemyPunching = false
On the next update let's look at what happens in the hero update:
Hero Update()
Check bIsEnemyPunching
bIsEnemyPunching was reset in the previous frame, so it is false
Since the enemy isn't punching don't do anything
So Hero never sees the punch because bIsEnemyPunching got reset before Hero had a change to check it.
This is why the order of adding the scripts matters. Whichever script updates first is able to punch because the second script will see the trigger before it gets reset. However, the second script to update will never be able to punch because the trigger gets reset before the other scripts gets a chance to update.
One solution is to check the name of the animation state instead of the trigger value.
static int punchStateHash = Animator.StringToHash("Punch");
AnimatorStateInfo enemyState = eAnim.GetCurrentAnimatorStateInfo(0);
if (enemyState.nameHash == punchStateHash){
//The enemy is punching!
}
Alternatively, whenever a punch is triggered just call a function on whichever character is getting punched. So when the enemy punches the hero the Punch(...) function on the enemy should call TakePunch(...) on hero (or whatever you want to call those functions). The hero then checks his own state to determine if he is blocking.
Additional Note
You should avoid using the following code in an update function:
GameObject player = GameObject.Find("theDude");
Animator pAnim = player.GetComponent<Animator>();
These functions are very slow because Unity must search all objects for the one called theDude and then search all it's components to find the animator. Do this once and save off the result.

Categories

Resources