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 timo0060 · May 10, 2012 at 02:14 PM · 2dmouseshooting

Shooting Towards mouse Cursor

I have a sphere in my game that follows the cursor and rotates around my player, but how would I have it so when I press the mouse button (Input.GetMouseButton(0)) is clicked, it fires to the position of the cursor? I'm confused on how to actual move it. I've looked around on here but didn't find anything that worked for me. My bullet prefab is also a rigidbody. Here's how I find the position of the mouse in my 2d game world (X-axis & Y-axis):

 var plane = Plane(transform.position, Vector3.up);    
 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);    
 var distance: float;
 if (plane.Raycast(ray, distance)){        
       hitPoint = ray.GetPoint(distance);
       hitPoint.z = -19;
     }

This is all done in the function Update(). Thank you for your help.

Comment
Add comment · Show 6
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 syclamoth · May 10, 2012 at 02:25 PM 0
Share

You're mostly there- once you have the 'HitPoint', you can get the direction that the object needs to be fired in using

 var direction : Vector3 = hitPoint - transform.position;

Then, just use whatever system you may want to use for your bullets, and that vector.

avatar image timo0060 · May 10, 2012 at 05:04 PM 0
Share

I can't quite wrap my $$anonymous$$d around how to do that. I just don't fully understand how to implement the vector with the transform.translate of my bullet.

avatar image Julien-Lynge · May 10, 2012 at 05:48 PM 0
Share

You may want to look more at rigidbody. You could do something like setting the velocity based on the desired vector, or apply a force in that direction.

avatar image timo0060 · May 10, 2012 at 06:28 PM 0
Share

Thank you for your advice. I'll certainly look into it. Thank you both.

avatar image syclamoth · May 11, 2012 at 03:26 AM 0
Share

Use 'Transform.translate(direction speed Time.deltaTime);'

it's not that hard.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tw1st3d · May 11, 2012 at 01:07 AM

I had a similar problem, although I was not looking for the object to go to where the mouse is. This is what I came up with however.

     var bullet : Rigidbody; 
     var speed : float = 10.0f;
     var muzzlePoint : Transform;
 
 function Update() {
     if(Input.GetButtonDown("Fire1")) {
     bullet.useGravity = false;
         var instance : Rigidbody = Instantiate(bullet, muzzlePoint.position, muzzlePoint.rotation);
         instance.velocity = muzzlePoint.forward * 50;
     }
 }

I hope this helps.

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 timo0060 · May 11, 2012 at 01:46 AM 0
Share

This code doesn't exactly go to the mouse though does 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

7 People are following this question.

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

Related Questions

Sidescroller Bullets Follow Mouse After Being Shot 2 Answers

How do I get my character to shoot a projectile in the direction of the cursor? 1 Answer

Shooting at mouse position in 2d from a Child Object 0 Answers

Shooting projectiles in 2D world 2 Answers

shooting in 2d problem 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