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 tklarenbeek · Mar 16, 2013 at 08:35 PM · shootingbulletgunprojectile

Help with getting a bullet to move.

I have a short script that is meant to spawn a bullet at the position of an empty object at the end of a gun and shoot it forward. However, while the bullet does spawn, it just sits there, and gives me this error:

InvalidCastException: Cannot cast from source type to destination type. Gun.Fire ()(at Assets/Scripts/Gun.js:12)

Here's my code:

 var Bullet : Rigidbody;
 var Spawn : Transform;
 var BulletSpeed : float = 1000;
 
 function Update () {
     if(Input.GetButtonDown("Fire1")){
         Fire();
     }
 }
 
 function Fire(){
     var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
     bullet1.AddForce(bullet1.transform.forward*BulletSpeed);
 }
Comment
Add comment · Show 5
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 robertbu · Mar 16, 2013 at 09:17 PM 0
Share

I just tested your code, and it worked fine. I could not reproduce the error.

avatar image tklarenbeek · Mar 16, 2013 at 09:27 PM 0
Share

Well, I'm following a tutorial on YouTube, and some people have been reporting the same problem, so it hasn't been just me. Could this possibly be due to the version of Unity I'm using (4.0)?

avatar image KingKongFu · Mar 16, 2013 at 09:33 PM 0
Share

Did you attach this code to something like the camera or a FPS perfab or something. Just off the top of my head I have done that many many times before

avatar image tklarenbeek · Mar 16, 2013 at 09:39 PM 0
Share

This code is attached to the object serving as a gun, in this case a cube, which is attached to the a weapon camera. Here's what the Inspector of it looks like:

alt text

Here's where it is in the hierarchy:

alt text

avatar image robertbu · Mar 16, 2013 at 09:58 PM 0
Share

I don't doubt you are having an error, and it is unlikely but possible to be the version of Unity. But if I cannot reproduce it using your script, it indicates a more subtle problem than the typical errors of this type. I'm running Unity version 4.01f2.

What happens if you use a different prefab for the bullet? Any chance the bullet script (if any) is deleting the rigidbody? What happens if you change your code to do this:

    var bullet1 : GameObject = Instantiate(Bullet,Spawn.position,Spawn.rotation);
     bullet1.rigidbody.AddForce(bullet1.transform.forward*BulletSpeed)

;

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tklarenbeek · Mar 16, 2013 at 10:08 PM

I figured it out based off of KingKongFu's comment. Simply moving the code to be on the First Person Controller instead of the gun fixed it.

Comment
Add comment · 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
0

Answer by tklarenbeek · Mar 16, 2013 at 10:14 PM

I managed to figure it out. Turned out all I had to do was take the script off the gun and then just put it back on again.

Comment
Add comment · 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

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting & Animation Problem 1 Answer

Control amount of bullets 2 Answers

Shooting multiple bullets 2 Answers

Bullet Collision Issues 1 Answer

I Want To Shoot Bullets Correctly. 2 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