- Home /
Instantiate bullet hitting me when accelerated
hi all, i need help with this, it confuses me. i have ship with weapon that shoot bullet front of ship, all is ok unless i move then bullet hit me. i don't know why doing it because when bullet is created it have speed bigger how ship but it look like position where the bullet is created is closer to ship with increasing speed. here is script what i use.
#pragma strict
var my_pos : Transform;
var ammo : GameObject[];
static var spawnDelay = 0.1f;
var time = 0f;
static var Pspeed = 0f;
function Start () {
}
function Update () {
}
function FixedUpdate () {
time += spawnDelay;
if(Input.GetKey(KeyCode.Mouse0)){
if(time > 1){
var ammoIndex = 0;
var bullet : GameObject = Instantiate(ammo[ammoIndex], transform.position, transform.rotation) ;
bullet.transform.LookAt(my_pos);
bullet.transform.rotation = transform.rotation;
bullet.rigidbody2D.velocity = transform.up * Pspeed; //i added here player speed because i dont want to be faster how bullet.
time = 0;
}
}
}
this is weapon script it create bullet front of me(weapon is empty gameobject added to front of my ship and there is bullet created).
#pragma strict
var bulletSpeed : float;
function Start () {
}
function Update () {
Destroy(gameObject,1.2);
transform.Translate(Vector3.up * bulletSpeed * Time.deltaTime); //and this is basic speed of bullet.
}
function OnCollisionEnter2D(){
Destroy(gameObject);
}
and this is bullet.
You are doing some strange things here.
$$anonymous$$ove the bullet either by setting rigidbody.velocity, or by using transform.Translate Don't use both in the same game.
i only try doing it this way but nothing is change it doing same when i have only one but one thing is when bullet dont have transform.translate and i change rigidbody.velocity for transform.translate it dont work, it only spawn bullet front of me and it dont have speed.
Do you have a rigidBody(not 2D) attached to your bullet. Also there is no reason to put Destroy(gameObject,1.2); in Update. Ins$$anonymous$$d, it must be in Start (you do not want ~200 timers in a line, waiting to destroy your bullet).
If your game is 2D then it is right to have rigidbody2D (I just thought it is 3D).
Answer by Ejlersen · Jul 29, 2014 at 07:46 PM
Here are my thoughts on the problem.
Use Update(), instead of FixedUpdate() to spawn your bullet.
Why? Because of execution order of physics and game logic. FixedUpdate is within physics update, where as Update() is after. You can see execution order here: http://docs.unity3d.com/Manual/ExecutionOrder.html
Set rigidbody2d.velocity on instantiation and not both velocity and translate.
Set OnDestroy on instantiation and not in Update(). Once will suffice.
ah big thx Ejlarsen! this change FixedUpdate for Update work, i had to change it on player script and on weapon must FixedUpdate had to stay.
Also, to stop collision with your ship, you can add the speed of the ship to the velocity of the bullet (will not stop collisions when there is torque or wneh you are speeding, but.. it is the same in the real world....).
Your answer