Hi! I want to give the enemies in my game the ability to shoot towards the player. How could I make a ball cannon that correctly aims at the player? Thanks!
Hi @Cryptonaph! Did you try to search on the forum first? There are many topics about shooting and creating a bullet.
Yes, but I couldn’t find one that suited what I was looking for.
So I tried some stuff, and I think I know how I would do it, however I’m getting an error that doesn’t help me.
[playcanvas-1.57.1.dbg.js:70233]: Cannot read properties of undefined (reading 'x')
The code on the bullet:
var EnemyAttack = pc.createScript('enemyAttack');
// initialize code called once per entity
EnemyAttack.prototype.initialize = function() {
};
var totalTime = 0;
// update code called every frame
EnemyAttack.prototype.update = function(dt) {
if (this.entity.enabled == false) {
dt = 0;
}
if (this.entity.enabled == true) {
this.entity.rigidbody.enabled = false;
this.entity.rigidbody.enabled = true;
totalTime += dt;
if (totalTime >= 20) {
totalTime = 0;
this.entity.destroy();
}
var force = new pc.Vec3(0,0,0);
force.copy(this.entity.forward).scale(100);
this.entity.rigidbody.applyForce(this.force);
}
};
Where am I doing this wrong?
Goal: When bullet is enabled, it flies in the direction it was fired, and then despawns after 20 seconds.
Wait I might see the problem…
Ok! I’m getting a new error now, progress!
[enemyAttackTest.js?id=105744784&branchId=a2499255-7868-4778-b13c-4edec2ef54f9:23]: Cannot read properties of undefined (reading 'applyForce')
I made it check for a rigidbody first, there is very clearly a rigidbody here, why is it not registering that?
Can you share a link of your project so someone can take a look?
The problem is probably that the rigidbody you use is kinematic. If you want to use forces, the rigidbody need to be dynamic.
I thought you could use forces on kinematics to move them, what should I do for the bullet to have it float towards the player like an orb?
The main thing I want is for it to not have gravity effect it, and for it to just go to where the player was when the bullet was fired. How can I do this? Currently with them being dynamic, I can tell they are cloned, but the render doesn’t move with it and in turn is invisible, and it seems it isn’t moved? The enemy just walks right over it. How can I make the bullets not collide with the enemy who fired it?
You can use something like below in the update function of your script. (Where 1
is the right bullet speed on the axis you need to move it).
this.entity.translateLocal(0, 0, 1);
I don’t see where you check for collision?
Oh, I don’t currently have it check for collision with bullets, I just want to know how to make the bullets not collide with the enemy it fires from before I do that.
Something like below.
if (result.other.name != 'enemy') {
// hit
}
Alright, is there any way to make the bullet go through other enemies?
If every enemy has the same name, the example above should be enough.
Alright, the reason why was because if the enemy is moving and comes in contact with a bullet, they teleport on top of it, I just want them to ignore it completely.
The bullets don’t register any hits with the player, they just go right through.
Nevermind, I had to teleport the rigidbody to the position after translating it locally.