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 slayer29179 · Mar 10, 2012 at 07:27 PM · cameraraycastscreenbulletfire

Fire bullet to mouse position in 3d space

Hey all, i am currently trying to detect the mouse position and fire my bullet towards it, i am not 100% sure it can actually be done (i really hope it can) a good reference would be some space fighting games such as Freelancer, i have had a look at Ray-cast which i don't understand but i tried some scripts i found off here, sadly the ones i looked at were for 2d games such as side scrollers, here is the code I am using currently;

 var character : Transform; //main character
 var bullet : Transform; // the bullet prefab
 
 function Update()
 {    
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 100)) 
     {
       // Now you have where your player is and a 3D point the mouse is over
       Debug.DrawLine (character.position, hit.point);
     }
 
     if(Input.GetMouseButtonDown(1))
     {
         var projectile = Instantiate(bullet, GameObject.Find("oneSpawn").transform.position, Quaternion.identity); 
         projectile.transform.LookAt(hit.point); 
         projectile.rigidbody.velocity = projectile.transform.forward * 10;
     }
 }

I am finding it hard to try and explain what I want but I don't want it to be as simple as they just click on the enemy's to fire, but where ever the mouse cursor is the bullets will fire forward towards it

thank you to anyone that can solve my problem because I don't know what to search for if I don't believe its even possible

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
3
Best Answer

Answer by aldonaletto · Mar 10, 2012 at 09:27 PM

This code should work! Are you having any problem? I would only save the oneSpawn reference in a variable, and do the raycast only when if the button is pressed - both changes related to performance improvement:

 var character : Transform; //main character
 var bullet : Transform; // the bullet prefab
 private var spawnPt : GameObject; // holds the spawn point object
 
 function Update(){   
   if(Input.GetMouseButtonDown(1)){ // only do anything when the button is pressed:
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 100)){
       Debug.DrawLine (character.position, hit.point);
       // cache oneSpawn object in spawnPt, if not cached yet
       if (!spawnPt) spawPt = GameObject.Find("oneSpawn");
       var projectile = Instantiate(bullet, spawnPt.transform.position, Quaternion.identity); 
       // turn the projectile to hit.point
       projectile.transform.LookAt(hit.point); 
       // accelerate it
       projectile.rigidbody.velocity = projectile.transform.forward * 10;
     }
   }
 }
Comment
Add comment · Show 9 · 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 slayer29179 · Mar 10, 2012 at 09:45 PM 0
Share

thanks Aldonaletto for trying but still nothing, sorry for not mentioning the problem, it won't spawn, won't fire, nothing just nothing happands.

$$anonymous$$y previous script which spawned a bullet in front of the ship and sent it forward worked but wasn't what I needed, the character has a controller on, the bullet is in the prefab working with a trigger, I have equipped the script and all the variables, if it helps

avatar image slayer29179 · Mar 10, 2012 at 09:57 PM 0
Share

I added some debug notes to see where it gets and it won't go any further than the if (Physics.Raycast (ray, hit, 100)){ then the debugging stops

avatar image aldonaletto · Mar 11, 2012 at 01:51 AM 1
Share

Since you said ship, I suspect that you're clicking the empty space - and this won't work, because Raycast only returns true if some collider (at 100m or less, in this case) is hit.
If this is the case, a possible hack is to create a big plane (scale 10000,1,10000) in front of the ship at a big distance (5000, for instance) and delete its mesh renderer. Child the plane to the player, and remeber also to change the raycast to Physics.Raycast(ray, hit) to remove the distance limit.

avatar image slayer29179 · Mar 11, 2012 at 11:40 AM 0
Share

YAY!! it works :D thank you Aldonaletto, it was the plane method I I was trying to click on nothing :)

avatar image Alter · Jan 20, 2013 at 05:25 PM 0
Share

Hey guys, is it possible for someone to convert this into C#, please?

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

7 People are following this question.

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

Related Questions

Detect touch in screen at different resolutions 2 Answers

Raycast based on crosshair on screen 1 Answer

Raycasr in my fps? 1 Answer

How to engage an object if it's seen by my camera?? 1 Answer

Question about sending physics raycast from the camera 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