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 SohaibTheGame · Jul 20, 2013 at 09:23 PM · instantiatebulletgunshootfire

Setting bullet instansiate direction? help?

Hello, I'm making a medieval first person shooter, similar to the usual FPS but a magic wand instead for a gun, fireballs instead of bullets.

The fireballs are not as fast as bullets, so creating a prefab that travels with a medium speed and makes damage on collision is the best idea I can think of.

The problem is, i can't make the fireballs come of the wand to go to the center of the screen where the GUI for the cross that shows where the fireball will hit(i forget its name). This is the script, I attached the script to an invisible cube, so the fireballs get created at the top of the want and go forward, but the problem is they go to a different direction everytime

 var fireBall : Rigidbody;
 
 function Update () {
 
 if (Input.GetButtonDown("Fire1"))
     {
     Instantiate(fireBall, transform.position, transform.rotation);
     fireBall.AddForce(Vector3.forward * 10);
     }
 
 }

![alt text][1]

PLEASE HELP ME FIND A CORRECT WAY TO DO IT

Thank you [1]: /storage/temp/13333-screenshot_6.jpg

screenshot_6.jpg (113.9 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jul 20, 2013 at 09:38 PM

To make this work, you have to specify a distance from the camera. It can either be a set distance, or you can use Raycast() to determine the distance. In addition, you need to do the AddForce() to the object created, not to the prefab. Here is a bit of untested code:

 #pragma strict
  
 var fireBall : GameObject;
 var distance : float;
  
 function Update () {
  
 if (Input.GetButtonDown("Fire1"))
     {
     var hit : RaycastHit;
     var point : Vector3; 
     if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, hit)) {
         point = hit.point;
     }
     else {
         point = Camera.main.transform.position + Camera.main.transform.forward * distance;
     }
     
     var go = Instantiate(fireBall, transform.position, transform.rotation) as GameObject;
     go.transform.LookAt(point);
     go.rigidbody.AddForce(go.transform.forward * 1000.0);
     }
 }

'distance' is the default distance in front of the camera to use as a point if the Raycast() fails.

Comment
Add comment · Show 2 · 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 SohaibTheGame · Jul 20, 2013 at 11:20 PM 0
Share

returns a NullReferenceExeption , still trying to figure out the solution :(

avatar image robertbu · Jul 21, 2013 at 04:08 AM 0
Share

I made a couple of changes. Note you will need to reinitialize (drag and drop again) the fireBall variable since I changed its type.

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

15 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 avatar image avatar image avatar image avatar image

Related Questions

Make a Gun fire multiple times 3 Answers

How can I shooting script c#? 0 Answers

Shooting Script Instantiate Problems. Help? 0 Answers

Audio when I shoot 3 Answers

My gun won't fire. ( Javascript ) 1 Answer


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