- Home /
rigidbody bullets dont spawn where i want them too.
i created a sphere named "bulletspawnPoint" and set its colliders to unchecked. it is also not rendered. what i want is for my bullet to instantiate at the location of the invisible sphere. i tried many tutorials but they all mess up some how even if i copy and paste... so i tried making my own code with the bits that DO work right, now i just need help positioning the bullets(that do shoot) to shoot from specifically the location of the bulletspawnPoint. this is my current code.
Bullets DO fire, just they spawn lower than intended and even when i try to set it to spawn elsewhere it still spawns here or errors. any tips?
var projectile : Transform;
var bulletSpeed : float = 20;
function Update () {
if(Input.GetMouseButtonDown(0)){
//Instantiate the projectile at the position and rotation of this transform
var clone : Transform;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Add force to the cloned object in the object's forward direction
clone.rigidbody.AddForce(clone.transform.forward * bulletSpeed);
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
What error? Sounds like you have multiple spawn scripts.
Never ever omit error messages. :P
rather than an actually "error" i ment it simply didnt do what it should, there is no error message being logged.
Answer by whydoidoit · Jun 22, 2012 at 03:20 PM
Use the code below - with the inspector drag and drop your spawn point onto the spawn point variable in the inspector.
var projectile : Transform;
var spawnPoint: Transform;
var bulletSpeed : float = 20;
function Update () {
if(Input.GetMouseButtonDown(0)){
//Instantiate the projectile at the position and rotation of this transform
var clone : Transform;
clone = Instantiate(projectile, spawnPoint.position, transform.rotation);
// Add force to the cloned object in the object's forward direction
clone.rigidbody.AddForce(clone.transform.forward * bulletSpeed);
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
it works perfect as far as the eye can tell, i get an error on line 35 which is completely blank in my script. it says "NullReferenceException: Object reference not set to an instance of an object."
ok i deleted the empty line 35 and it is now the "old" line 36. so now its a //comment line. however oddly enough it still says its an error on line 35. o_0
So you are sure that you have used the inspector to set both projectile and spawnPoint? To what is this script attached?
Yes, im 100% sure. The Prefab "FPSInput Controller" that comes with unity. the set up in my Hierarchy is as follows:
First Person Controller
Graphics
$$anonymous$$ain Camera
bulletspawnPoint
Gun 3D $$anonymous$$odel
all gun model Pieces (barrel, trigger, etc)
the script is written inside the First person controller script. just as you had written, and properly placed inside the inspector with my "bullet" object as the projectile, my bulletspawnPoint as the Spawn Point, Bullet speed is 1000, and there is a rigidbody on my bullet.
And you are sure that the error is reporting from the non existent line in this script?
Your answer
![](https://koobas.hobune.stream/wayback/20220613073135im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bullet Spawn 3 Answers
FPS Instantiate position. Please help? 0 Answers
Projectile instantiation positioning is off. 3 Answers
Spawn a bullet of a cannon at right position 1 Answer
Which is better, shooting a bullet from camera or from the gun itself? 3 Answers