- Home /
Why does the bullet has wrong direction?
I have this script attached to a gun, that shoots a sphere(bullet) from an empty GameObject named "Spawner" in front of it. The problem is that when I press the mouse button, the bullet has a wrong direction, as the image shows bellow. Why is this happening?
Preview:
This is the script that I use:
#pragma strict
var bulletPref : Rigidbody;
var bulletPos : Vector3;
var spawner : GameObject;
function Start () {
}
function Update () {
bulletPos = spawner.transform.position;
if(Input.GetMouseButtonDown(0))
{
var clone : Rigidbody;
clone = Instantiate(bulletPref, bulletPos, transform.rotation);
clone.velocity = transform.TransformDirection (Vector3.forward * 20);
}
}
Answer by aldonaletto · Jan 20, 2013 at 02:58 PM
The bullet should go in the forward direction of the object to which this script is attached (Pistol, I presume). If it's being deviated, maybe the bullet collider is touching some other collider at the spawner position (the player, the pistol model etc.)- this would result in an altered trajectory.
NOTE: Usually, in a first person game the weapon is childed to the camera, so that the player shoots in the direction he's looking at.
It doesn't collide with anything, I have tried to put the spawner in a lot of positions, but always it shoots in the wrong diraction :(
Weird thing! It should follow in the forward direction of the script owner object. Try to add a debug line in Update:
clone.velocity = transform.TransformDirection (Vector3.forward * 20);
Debug.DrawRay(clone.velocity, Color.red, 2);
This draws a red line for 2 seconds in the direction the bullet should go. NOTE: Click the button gizmos in the Game view, or no lines will appear.
Thanks, but I solved it, I had a script attached to the bullet that affected the direction!!!
Your answer
Follow this Question
Related Questions
Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer
Turning a gameobject's gravity on/off 1 Answer
Can't instantiate a rigidbody 1 Answer
Increase enegy before shooting 1 Answer
How can I instantiate rigid bodies on top of each other without them exploding away? 2 Answers