- Home /
Trouble getting bullets to stop at solid blocks
I'm currently working on my first game (a 3D side scroller. It's a parody of Super Mario Bros) and until now I've been able to find answers via google searches, but now I've stumbled across a problem...
To avoid any nasty pushing-around physics, the bullets in my game (shirukens or fireballs, currently just shirukens) have a box collider and is set to a trigger, but no character controller. To move, it uses transform.position. Anyway, it collides with enemies and other triggers just fine, but when I try to hit a solid object (whether it be plane or cliff box), it doesn't process any of those collisions.
Code attached:
#pragma strict
private final var gravityRate : double = 25.0;
private final var gravityMax : double = 100.0;
private final var deathTimeMax : double = 0.5;
private var gravity : double = 0.0;
private var deathTime : double = 0.0;
var direction : Vector3;
var throwSound : AudioSource;
var hitSound : AudioSource;
function Start () {
Debug.Log("Creating a g**d*** shiruken");
throwSound.Play(0);
}
function Update () {
if(deathTime > 0.0)
{
//Give the sound effect some time to work it's magic
deathTime -= Time.deltaTime;
if(deathTime <= 0.0)
{
deathTime = 0;
Destroy(this.gameObject);
}
return;
}
gravity += gravityRate * Time.deltaTime;
if(gravity > gravityMax)
gravity = gravityMax;
var offset : Vector3 = transform.position;
offset.x += (direction.x * 30) * Time.deltaTime;
offset.y += (direction.y * 15) * Time.deltaTime;
offset.y -= gravity * Time.deltaTime;
transform.position = offset;
}
function OnTriggerEnter(collision : Collider)
{
var other : GameObject = collision.gameObject;
Debug.Log("OUCH! " + other.tag);
if(other.tag == "Enemy")
{
deathTime = deathTimeMax;
hitSound.Play(0);
var script : EnemyDamage = other.GetComponent(EnemyDamage);
if(script != null)
script.Damage();
}
}
Answer by MartinCA · May 11, 2013 at 12:30 AM
Do your projectiles or walls have rigidbodies? Collision and Trigger events will only raise when one of the objects has a rigidbody component.
You can view the collision and trigger event matrix here (scroll to the bottom): http://docs.unity3d.com/Documentation/Components/class-SphereCollider.html
Also, if you want to move rigidbodies, refer to this - http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.MovePosition.html