Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Punkjim420 · Jun 22, 2012 at 03:18 PM · instantiatepositionfpsbullet

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")

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Loius · Jun 22, 2012 at 03:23 PM 0
Share

What error? Sounds like you have multiple spawn scripts.

Never ever omit error messages. :P

avatar image Punkjim420 · Jun 22, 2012 at 03:27 PM 0
Share

rather than an actually "error" i ment it simply didnt do what it should, there is no error message being logged.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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")
Comment
Add comment · Show 20 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Punkjim420 · Jun 22, 2012 at 03:27 PM 0
Share

ok, ill try that. and let you know what happens.

avatar image Punkjim420 · Jun 22, 2012 at 03:35 PM 0
Share

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."

avatar image Punkjim420 · Jun 22, 2012 at 03:39 PM 0
Share

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

avatar image whydoidoit · Jun 22, 2012 at 03:42 PM 0
Share

So you are sure that you have used the inspector to set both projectile and spawnPoint? To what is this script attached?

avatar image Punkjim420 · Jun 22, 2012 at 04:08 PM 0
Share

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.

avatar image whydoidoit Punkjim420 · Jun 22, 2012 at 04:10 PM 0
Share

And you are sure that the error is reporting from the non existent line in this script?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges