- Home /
Fighting collision
I'm making a fighting game and I had set up a code
public class Fight : MonoBehaviour {
public float Damage=60;
public List<Transform> HitList;
private bool hit;
public bool YouWon;
public bool ko;
private bool dealdamage;
internal Animator animator; //stores the animator component
float v; //vertical movements
float h; //horizontal movements
float straight;
float hook;
float block;
float body;
float jump;
float left;
float right;
float high;
float wide;
void Start (){
animator = GetComponent<Animator>(); //assigns Animator component when we start the game
}
void Update (){
v = Input.GetAxis("Vertical");
h = Input.GetAxis("Horizontal");
straight = Input.GetAxis("Straight");
hook = Input.GetAxis("Hook");
high = Input.GetAxis("Guard Height");
wide = Input.GetAxis("Guard Width");
block = Input.GetAxis("Block");
body = Input.GetAxis("Body");
TurnLeft();
TurnRight();
Jump();
Stats php=(Stats)GetComponent("Stats");
if(php){
if(php.CurrentHealth<=0)ko=true;
}
//LETS DO SOME KILLIN!
if(hit){
if(HitList.Count>0&dealdamage){
int ls=HitList.Count;
for (int i = 0; i < ls; i++){
Stats hp=(Stats)HitList[i].transform.GetComponent("Stats");
hp.CurrentHealth=hp.CurrentHealth-Damage;
if(hp.KO){}
}
dealdamage=false;
}
}
}
void FixedUpdate (){
//set the "Walk" parameter to the v axis value
animator.SetFloat ("Walk", v);
animator.SetFloat ("Turn", h);
animator.SetFloat ("Straight", straight);
animator.SetFloat ("Hook", hook);
animator.SetFloat ("Block", block);
animator.SetFloat ("Body", body);
animator.SetFloat("Turn Left", left);
animator.SetFloat("Turn Right", right);
animator.SetFloat("Guard Height", high);
animator.SetFloat("Guard Width", wide);
animator.SetFloat("Jump", jump);
}
void TurnLeft (){
if(Input.GetButton("Turn Left")) {
left = 0.2f;
}
else {
left = 0.0f;
}
}
void TurnRight (){
if(Input.GetButton("Turn Right")) {
right = 0.2f;
}
else {
right = 0.0f;
}
}
void Jump (){
if(Input.GetButton("Jump")) {
jump = 0.2f;
}
else {
jump = 0.0f;
}
}
void OnCollisionEnter(Collider other){
//ADD AIS TO LIST TO INFLICT DAMAGE IF IN RANGE
Health AI=(Health)other.transform.GetComponent("Health");
if(AI){
HitList.Add(other.transform);
}
}
void OnCollisionExit(Collider other){
//REMOVE FROM LIST WHEN OUT OF RANGE
Health AI=(Health)other.transform.GetComponent("Health");
if(AI){
HitList.Remove(other.transform);
}
}
void OnGUI(){
//HEALTH BAR AND AI COUNT
Health php=(Health)GetComponent("Health");
if(php){
float hpp=php.CurrentHealth;
GUI.Button(new Rect(0, 30, 300, 26), "Health: "+hpp);
}
//YOU WON!
if(YouWon){
GUI.Box(new Rect(200, 200, 330, 260), "Congratulations! You have defeated all the Evil Skellies!");
if(GUI.Button(new Rect(310, 400, 120, 26), "Continue Playing"))YouWon=false;
}
}
}
How can I get the fighter to detect enemy collision?
First of all, consider splitting all those things into multiple separate scripts. You're probably going to add a few more things and the more you add, the more confusing the entire script gets. Splitting it into a few different scripts with clear defined purposes will help you keep control of your scripts.
The second thing is, what exactly do you mean by "detect enemy collision"?
When the fighter throws the punch I want the enemy model to the detect the collision.
For more information on when which method gets called, see this site.
Answer by raja-bala · Sep 30, 2014 at 12:17 AM
Too much code to look at :) So, add a few Debug.Log statements in the OnCollision* methods and see if they get called. If not, you have an issue in your collider/rigidbody setup. The gist is that Unity will NOT detect a collision b/w two objects with colliders where none of them have a rigidbody component. If you want to have it that way, you'll have to explore the OnTrigger* methods.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to call OnTriggerEnter once 5 Answers
2D box colliders not touching but are colliding, how to fix? 0 Answers
Weapon System with collide detection (Helps with script pls)!!! 0 Answers