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
1
Question by FuzzieLogick · Oct 15, 2012 at 01:38 AM · raycastprojectilefiremouse position

Fire Raycast Toward Current Mouse Position

Currently I have a fixed camera and a reticule which follows the position of the mouse onscreen. The reticule script is attached to an otherwise empty game object and uses GUI.DrawTexture. The raycast script is attached to the camera object.

I want to eventually throw a ball projectile from the camera's position towards the mouse cursor's position (ie. the reticule) upon pressing the spacebar. I am trying to accomplish this by using a raycast which can then return a position that I can hopefully instantiate the ball prefab towards. I'm pretty new to Unity so I'm not even sure that this is the right way to go about it. I did try to use Instantiate alone to fire from the camera towards the mouse cursor but couldn't get it to work.

The relevant part of my script is below (I did have force added for colliding with rigid bodies amongst other things but I thought it best to minimise how much I pasted in here). When I hit play I can see the line for the raycast being drawn straight ahead from the camera so it does work. However, I'm not sure what to use to get the ray to fire towards the mouse position. I have tried making the rayDirection variable Input.mousePosition but this stops the line from being drawn.

Thanks in advance for any advice and here is the stripped down version of the script which just draws a line straight from the camera. I need to know whether it's possible to adapt this to go towards the current mouse position:

 // Declare variables
 var rayRange:int = 1000;
 
     function Update () {
     
         if(Input.GetKeyDown(KeyCode.Space)){
         
             FireRay();
         
         }
     
     }
     
 // Define functions
 
 function FireRay(){
 
     var rayDirection = transform.forward;
     
         Debug.DrawRay(transform.position, rayDirection * rayRange, Color.cyan);
 
 }
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
Best Answer

Answer by sparkzbarca · Oct 15, 2012 at 01:46 AM

the mouses position is

Input.mouseposition Thats it, unity knows it for you.

to find the direction from the camera to the mouse. its where your going - where your coming from.

 Direction = Input.mouseposition(the to) - camera.transform.position(the from)
 
 
 ray.origin = camera.transform.position
 ray.direction = direction (see calculation above)

But you dont actually want to cast a ray.

You want the direction from camera to mouse which I already told you how to find so spawn from camera with it heading towards direction.

best of luck and mark as answered!

Comment
Add comment · Show 1 · 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 FuzzieLogick · Oct 16, 2012 at 06:53 PM 0
Share

Thanks for this :) I will let you know how it goes!

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

10 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

Related Questions

Projectile Firing From Floor, I Need Some Help 1 Answer

3D Space Shooter Firing Projectiles 0 Answers

Error with gun aim to the mouse 1 Answer

Projectile Script Works Until Destroying Projectile 0 Answers

Physics.Raycast (ray, hit) strange behaviour 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