- Home /
Bullets going wrong direction
No matter what I do the bullets either go left ( When doing rigidbody.velocity(transform.forward * bulletSpeed) or right when - forward, Up with Transform.Up Down with -Transform.Up... I've rotated the Instaniate position to almost every angle and no difference.
Good luck and please help.
function Fire()
{
if (Time.time > fireRate)
{
Instantiate(gasP, gameObject.Find("GasCham2").transform.position, gameObject.Find("GasCham2").transform.rotation);
var bulletP = Instantiate(bullet, gameObject.Find("GasCham").transform.position, gameObject.Find("GasCham").transform.rotation);
bulletP.rigidbody.velocity = (transform.forward * bulletSpeed);
ammoInClip = ammoInClip - 1;
fireRate = Time.time;
lastShot = Time.time;
}
}
What object is this component in? Because you are using the forward
of that object's transform
, not of the bullet, to deter$$anonymous$$e the velocity. In the Unity Scene view, select that object, make sure the tool in the top left says "Local" not "Global", and look where the BLUE (Z) axis goes. That's what transform.forward
is to your code.
Answer by aldonaletto · Jun 27, 2011 at 04:08 AM
Try to use the bullet's forward direction, which is the same as the GasCham object - if needed rotate the GasCham object in the Editor to point to the correct direction. By the way, your fire rate control doesn't work. That's the fixed script (including bullet direction):
var fireRate:float = 5; // shots per second
private var nextShot:float = 0;
function Fire()
{
if (Time.time >= nextShot)
{
Instantiate(gasP, gameObject.Find("GasCham2").transform.position, gameObject.Find("GasCham2").transform.rotation);
var bulletP = Instantiate(bullet, gameObject.Find("GasCham").transform.position, gameObject.Find("GasCham").transform.rotation);
bulletP.rigidbody.velocity = (bulletP.transform.forward * bulletSpeed);
ammoInClip = ammoInClip - 1;
nextShot = Time.time+(1/fireRate);
}
}