Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by valaira · Nov 24, 2015 at 01:08 AM · c#unity 5mousebulletmouseposition

Bullet to Mouse Position?

Hi;

I've got some code that only fires in a straight line and I can't figure out for the life of me how to get it to the mouse position. Can anyone help ?

 public Rigidbody projectile;
 public float speed = 20;

 void Update () 
 {

     if (Input.GetButtonDown("Fire1"))
     {
              var pos = Camera.main.WorldToScreenPoint(transform.position);
              var dir = (Input.mousePosition - pos).normalized;

              Rigidbody instantiatedProjectile = Instantiate(projectile,
                                                 transform.position,
                                                 transform.rotation)
                                                 as Rigidbody;

              instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed)); 
     }
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 say_forever · Nov 24, 2015 at 02:35 AM

 Ray rayFromCam = Camera.main.ScreenPointToRay(Input.mousePosition);
 if (Physics.Raycast(rayFromCam, out hitFromCam))  
 {
     hitFromCam.point  //this method return Vector3
 }

Hope this is what you need

Comment
Add comment · Show 6 · 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 valaira · Nov 24, 2015 at 03:58 AM 0
Share

all seems fine, but the hitFromCam.point is throwing up an assignment, call error

avatar image say_forever valaira · Nov 24, 2015 at 09:19 AM 0
Share

because it would return Vector3 to let you use. like show information Debug.Log(hitFromCam.point);

avatar image valaira say_forever · Nov 24, 2015 at 09:19 PM 0
Share

How would this be fixed then? Sorry I'm kind of a program$$anonymous$$g 'noob' and just trying to understand it at the same time

Show more comments
Show more comments
avatar image say_forever valaira · Nov 30, 2015 at 08:15 AM 0
Share

first, you double define RaycastHit hit; then, hit.point will return a vector3, so you need to use it.

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

43 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 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 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

Mouse position from new InputSystem is not correct and is not the same as Input.mousePosition 0 Answers

Bullet spawning at high Player speed 1 Answer

Can't get a gameObject to follow my mouse 0 Answers

Return key triggering a button OnClick? 1 Answer

How do i get my bullet to shoot where my mouse is pointed and at the same constant speed. unity 5 c# line 60 0 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