Need some help on coding

Zachy

Spark Of Life
Modder
Donor
Game Developer
May 6, 2017
695
1,727
Hey there! Thanks for opening this thread.

So... I've been working for the last days on a 2D platformer game like FOBS with some Megaman mechanics, but the thing is, my Javascript or C# level is potato, I don't know how to do anything without a tutorial.

What I need to do is creating some kind of variable that's off by default, but that can be enabled when the player collides with a trigger 2d collider. So, the thing I want is that when this variable turns on an animation is played (player bouncing back, tripping and falling to the floor.) , and that if any other enemy collides with him, it will trigger ANOTHER animation and turn off the variable.
Also, it would be nice if the player could turn off the variable by pressing Up when knocked out.

Greetings, and thank you!
 

ManicMeercat

New Member
Mar 6, 2018
2
2
Hey Zachy,

From what you've said this is the solution I've written up for you, please note that unity uses object oriented programming so this should be split into a script for the player and a script for the trigger, all of the solutions you requested are here but you should always endevour to study and solve issues on your own as such you should be able to take this apart and work out how to implement it into your code

//default state is off
public bool knockBack = false;
private bool knockedOut = false;

void Update(){
if (knockBack == true) {
//sets animator bool within unity animation system
GetComponent<Animator> ().SetBool ("Knockback", true);
}
if (knockedOut && Input.GetKeyDown(KeyCode.UpArrow)) {
knockBack = false;
}
}

// This is callend when an object enters a trigger2D
void OnTriggerEnter2D(Collider2D other){
//checks object is player
if (other.gameObject.tag == "Player") {
//sets bool on the player object
other.GetComponent<PlayerScript> ().publicknockBackbool = true;
}
}
//when colliding with other objects
void OnCollisionEnter2D (Collision2D other){
//checks to see if it is an enemy and already being knocked back
if(other.gameObject.tag == "Enemy" && Knockback == true){
//sets animator bool in unity animation system
GetComponent<Animator>().SetBool ("Knockback2", true);
}
}
 
  • Like
Reactions: Zachy and Palanto

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
992
If you are using C# does that mean you are using unity (it looks like ManicMeercat made the same assumption) having a special variable may not be needed. This is because if you are using Unity’s Mecanim visual scripter to organize and manage animation (the flow chart system) then by default “triggers” already do something similar. You turn on the trigger, the logic system checks to see if the trigger is on, if it is it clears it (turns it off) while also turning on the right animation. So it auto clears the trigger for you. and you can either reuse it or use a secondary trigger. Do note however, if the trigger is called, but the logic system does not see this as a legal move (say your crawling, and there is no connection between the crawl state and the animation you want, the system will ignore this trigger until you stand back up and the system sees you can now play the animation, it will then play the animation (late) and clear the trigger at the wrong time). secondly, you cound use a boolean variable in Mecanim as a transititon requirment, and when the animation plays, you can add a script to the state that clears the variable. Tho I do wish my main computer was working so i could actually show you how to do any of this.
 

Zachy

Spark Of Life
Modder
Donor
Game Developer
May 6, 2017
695
1,727
Hey Zachy,

From what you've said this is the solution I've written up for you, please note that unity uses object oriented programming so this should be split into a script for the player and a script for the trigger, all of the solutions you requested are here but you should always endevour to study and solve issues on your own as such you should be able to take this apart and work out how to implement it into your code
Hey bro, first of all, thank you for replying so quickly and programming that code. I also saw that this is your first message ever, so I aprecciate that.

Right now, I'm more focused on making the main character's spritesheet and the level design; so I won't be able to test your code right away. Buut since I'm able to kinda "read" the code (I can read but not create it) I don't see anything wrong with it; however, I'll try to PM you in the case something is missing.

If you are using C# does that mean you are using unity (it looks like ManicMeercat made the same assumption) having a special variable may not be needed.
Well... I remember tagging the thread with the Unity prefix, but I guess it got glitched.

And about your solution, I think it's pretty good but it may be kinda annoying or confusing to use with multiple animations and enemy IDs. Anyways, thank you!
 

ManicMeercat

New Member
Mar 6, 2018
2
2
If you are using C# does that mean you are using unity (it looks like ManicMeercat made the same assumption)
It was tagged with a unity tag when It was first uploaded which is why I replied using unity code and solutions.

Hey bro, first of all, thank you for replying so quickly and programming that code. I also saw that this is your first message ever, so I aprecciate that.

Right now, I'm more focused on making the main character's spritesheet and the level design; so I won't be able to test your code right away. Buut since I'm able to kinda "read" the code (I can read but not create it) I don't see anything wrong with it; however, I'll try to PM you in the case something is missing.
It wont work in it's current state as I mentioned in my earlier post, it has all the parts you mentioned, however would need separating into 2 separate scripts and should run fine, it should be easy to decipher with the comments and doing so will further your own knowledge of C# within Unity to make later endevours easier for yourself.
 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
992
yeah zachy also mentioned that, and after a day i could see that yellow unity tag thing.
 

Zachy

Spark Of Life
Modder
Donor
Game Developer
May 6, 2017
695
1,727
It wont work in it's current state as I mentioned in my earlier post, it has all the parts you mentioned, however would need separating into 2 separate scripts and should run fine, it should be easy to decipher with the comments and doing so will further your own knowledge of C# within Unity to make later endevours easier for yourself.
Okay then! Right now I'm making my own 2D character controller and I'm learning a lot in the process, so I think I'll be okay.