- Home /
Bullets sometimes shoot under map
hello! i am very new to unity and am trying to make a shooting scrip but at the moment about 2/10 shots that are fired go under the map but stay parallel to the bullets that shoot correctly if that makes sense.
here is the current code.
var projectile : Rigidbody;
var speed = 20;
function Update()
{
if( Input.GetButtonDown( "Fire1" ) ) {
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
}
}
Answer by clunk47 · Dec 16, 2012 at 11:09 PM
Try this. This will make sure the projectile is always instantiated in front of the player. I have optionally disabled the use of gravity, which you can always change. You also don't want the projectiles to collide with whatever the script is attached to. (Physics.IgnoreCollision) If you want to shoot the projectiles where you're actually aiming, attach this script to either the Main Camera, or attach to an empty game object at the end of a gun barrel...
var projectile : Rigidbody;
var speed : float = 20.0f;
private var fwd;
private var instantiatePosition : Vector3;
private var instantiatedProjectile : Rigidbody;
function Update()
{
fwd = transform.forward;
instantiatePosition = transform.position + fwd;
if(Input.GetButtonDown("Fire1"))
{
instantiatedProjectile = Instantiate(projectile, instantiatePosition, transform.rotation);
instantiatedProjectile.velocity = fwd * speed;
instantiatedProjectile.useGravity = false;
if(collider)
Physics.IgnoreCollision(instantiatedProjectile.collider, collider);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Shooting Accuracy Problem 0 Answers
Shooting speed help! 1 Answer
Control amount of bullets 2 Answers
Need help with shooting script 2 Answers