- Home /
Collider is only working on the first frame
I am trying to make a simple turn-based strategy game, but I am having some difficulty with the collider. The goal is to move the collider, instead of the player, into the enemy and once they collide the enemy takes damage and the collider moves back to center(0,0,0).
I have gotten it to work if I set the collider to already be colliding with the enemy at the beginning of the scene. If I try to move the collider either incrementally or all at once it seems like the collider is not being triggered, even though I can see the colliders overlapping in the scene view.
Both game objects have box colliders, rigid bodies, and are not kinematic.
On another note when I do get it working all values in the transform section keep increasing and not sure why.
I would really appreciate some help. Thanks
var attackDamage : int;
var turn: boolean;
var enemy : GameObject;
private var health : Health;
private var boxCollider : BoxCollider;
boxCollider = GetComponent(BoxCollider) as BoxCollider;
var col : float;
function Awake(){
health = enemy.GetComponent(Health);
}
function OnCollisionEnter (col : Collision)
{
if(col.gameObject.name == "Player2")
{
health.currentHealth--;
boxCollider.center = Vector3(0,0,0);
}
}
function Update (){
if (Input.GetKeyUp(KeyCode.UpArrow)){
//Try to see if I could incement it and it would not work
col += 0.1;
boxCollider.center = Vector3(0,col,0);
}
else if (Input.GetKeyUp(KeyCode.DownArrow)){
boxCollider.center = Vector3(0,-0.25,0);
}
else if (Input.GetKeyUp(KeyCode.RightArrow)){
boxCollider.center = Vector3(0.25,0,0);
}
else if (Input.GetKeyUp(KeyCode.LeftArrow)){
boxCollider.center = Vector3(-0.25,0,0);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613164622im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object Collision PLEASE HELP!!! 1 Answer
Adding varibales from other scripts 3 Answers
OnCollisionEnter Collision not detected? 1 Answer
Trigger Enter and Exit not working properly? 2 Answers
Generating A Custom Collider For Multiple Meshes - Script 2 Answers