Hello Guest

Author Topic: OnCollision not triggering with tk2d camera  (Read 5590 times)

Hapistorique

  • Newbie
  • *
  • Posts: 10
    • View Profile
OnCollision not triggering with tk2d camera
« on: December 09, 2012, 03:24:32 pm »
Hey,

I have an issue with the tk2d camera, I started using it yesterday and it's really strange :

All the sprites are really large, but the colliders are really small in Z :


Is it supposed to look like that ?

My main issue is with a collider, I'm simply making a fireball spell, when the fireball touch the ground or anything, it destroy itself
So I have this code :

Code: [Select]
void OnCollisionEnter(Collision collision){
Destroy(transform.gameObject);
Debug.Log("Fireball collided");
}

really simple for now, but it's not working, the collision is only detected sometimes, when the other sprite is moving  :-\
All the sprites and the ground cube are on the same layer

If this can help I'm using iTween to move the sprite
Code: [Select]
iTween.MoveTo(newFireBall.gameObject, new Vector3(curMousePosition.x, -0.1f, 0), 5 );
When using rigidbody and leting the object fall by itself to the ground it works fine
« Last Edit: December 09, 2012, 05:09:13 pm by Hapistorique »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: OnCollision not triggering with tk2d camera
« Reply #1 on: December 10, 2012, 11:39:40 am »
Yes thanks for posting the line you were using to move it - the issue has all to do with that :)

The thickness of the collider isn't the issue here, but you could certainly increase it. In sprite collection editor, go into settings, change collider depth. The default is 0.1 units, which is why it looks thin with with the tk2dCamera enabled.

Back to the issue, do you have a rigidbody attached to this, and is it set to kinematic?

Hapistorique

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: OnCollision not triggering with tk2d camera
« Reply #2 on: December 10, 2012, 07:25:37 pm »
No I'm using rigidbody with gravity, I should use kinematic ?

I have the exact same issue with arrows, they should disappear and send damage to the gameobject collided
When the ennemy is moving, it works fine, but when it's standing still it goes right thought it and doesn't trigger oncollisionenter

I tried using rigidbody + kinematic for all but it doesn't do much

EDIT : It seems like it works when using rigidbody without gravity or kinematic or gravity only

Arrow.cs : (moves the arrow, detects collision)
Code: [Select]
void Update () {
transform.Translate(new Vector3(0.01f,0,0));
}

void OnCollisionEnter(Collision col){
if(col.collider.CompareTag("Enemy")){
//col.gameObject.SendMessage("ChangeHealth", -archerScript.GetAttackPower());
Destroy(transform.gameObject);
}
}

Code to spawn an arrow :
Code: [Select]
public override void Attack(GameObject target){
if (curTarget != null && attackTimer >= attackDelay){
attackTimer = 0;
soundManager.SendMessage("PlayOnce", "meleeAttack");
tk2dSprite newArrow = Instantiate(arrow, new Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity) as tk2dSprite;
}
« Last Edit: December 10, 2012, 07:38:56 pm by Hapistorique »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: OnCollision not triggering with tk2d camera
« Reply #3 on: December 11, 2012, 01:15:29 am »
You can't move a non-kinematic rigid body with iTween, etc. If you did that you are likely to miss lots of (most) collisions. If you need to move a non-kinematic rigidbody, use Rigidbody.AddForce (http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html) instead.