- Home /
Cannon shoots in wrong direction with unrealisic Physic
I want to implement a realistic cannon shoot but the cannon balls are fired in wrong direction and seems like soap bubble despite metal material and my weight settings
Here a sample video of the result LINK
My intention is to shoot in the direction of the boat in position (-7,10.4,100).
The cannon is in the position (0, 10.39,-40)
Here the code to fire:
var Bullet_Active: Transform;
var bullet_energy=999;
function Start () {
}
function Update () {
if(Input.GetKey("space")){CannonFire(); }
}
function CannonFire () {
var bullet_istance :Transform;
bullet_istance=Instantiate(Bullet_Active, transform.position, transform.rotation);
bullet_istance.GetComponent.<Rigidbody>().AddForce(bullet_istance.transform.forward*bullet_energy);
}
Looks like a collider Problem. You need to disable collision bullets with bullets.
First change the collider of the bullet to IsTrigger and test again
If that is not what you want go to the physicsmanager and change the $$anonymous$$atrix to your needs, So make Bullets in a Bullet Layer and Disable each other (Edit > Project Settings > Physics)
Also limit how many bullets are instantiated.
the problem was in direction transform.forward
. The right direction is transform.up
in my case
Answer by screenname_taken · Jun 19, 2015 at 09:42 PM
You are shooting all the balls together at the same exact time and they are colliding with each other. Instead make a layer specific for the balls that is able to interact with everything but the balls, so that they won't interfere with each other. Then also make a script that has the balls in an array and implement a delay so that they won't shoot like pellets from a shootgun, but instead one ofter the other. They are shooting together because you have the check in the update loop, with no cooldown.
Put a boolean in the firing function that goes false the moment it's called and then back to true after some time and check that with an if statement to see if you should shoot, so that the balls will go "boom .... wait ... boom" instead of "splat".
Oh and try not to instantiate stuff, it takes cpu time. Instead have a bunch of them already in the scene the time it loads, but just disabled, and place them in an array. That way the system will just pick a free one, enable it, shoot it, and once it hits and does the damage you can disable it and make it available again.
Thanks for the answer however the main error was in direction transform.forward
. I must use transform.up
.