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 /
avatar image
0
Question by Marshal_Alessi · Jan 18, 2017 at 01:27 AM · shootingdirectionmouselooklaser

Look Vector of Mouse

I know this question has been asked before but I'm having an issue in my case that I cannot solve. I'm attempting to make a laser gun that shoots out where the user clicks on the screen. Part of the issue is that the laser is instantiated from one script, while the movement is handled in another one. I check for a mouse click(among other things) which does this:

 Instantiate(laserPrefab, shootFX.transform.position, Quaternion.identity);

That shoots it originating at the tip of the gun, and then in the laser script that's generated I have:

     void Update ()
     {
         transform.position += transform.forward * speed * Time.deltaTime;
     }

Now obviously that's written so it goes in a straight line, but that is not what I'm trying to accomplish. I tried a few other things, like creating a vector3 inside the laser with the mouses position and traveling in that direction, but I couldn't get it to work.

Comment
Add comment · Show 2
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 Bunny83 · Jan 18, 2017 at 02:52 AM 0
Share

Way too many open variables:

  • 2d or 3d?

  • if 3d is it a fix camera view so you have an actual mouse that you move around on the screen, or is it a first person view where you actually rotate the camera with your mouse?

The Quaternion.identity parameter makes your instantiated object being aligned with the world axis. Don't you simply want to rotate it according to your ai$$anonymous$$g direction?

If it's a 3d game and your "shootFX" is your actual spawn point, doesn't it's forward axis point in the right direction? You would simply pass shootFX.transform.rotation ins$$anonymous$$d of Quaternion.identity to rotate the instantiated object into the same direction. The code on the "laser" object is fine. It simply moves along it's local forward axis.

avatar image Marshal_Alessi Bunny83 · Jan 18, 2017 at 03:57 AM 0
Share

It's 3D, however I need it to be aligned with where the mouse is pointing. The shootFX is the tip of the gun, which does not move. This is a 3rd person game, so I need it to register toward the direction of the mouse click.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Jan 18, 2017 at 04:21 AM

Well, so it's a 3rd person game and the user actually have a mouse cursor and points to the place where you want to shoot. It however sounds a bit strange when you say "The shootFX is the tip of the gun, which does not move". Doesn't your character rotate and look in a certain direction? If the shootFX is a child of the gun and the gun a child of the character it would move and rotate with the character...

Anyways, you usually need those steps:

  • Create a ray from the camera along the mouse position. For this you simply use ScreenPointToRay with the current mousePosition.

  • Use Physics.Raycast and determine the world space hit point where your mouse is over.

  • Subtract your starting position from the hit point to get a direction vector.

  • Use Quaternion.LookRotation() to create the proper rotation which you can pass to Instantiate.

Something like that:

 // inside Update
 if (Input.GetMouseDown(0))
 {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if (Physics.Raycast(ray, out hit))
     {
         Vector3 pos = shootFX.transform.position;
         Vector3 dir = hit.point - pos;
         Instantiate(laserPrefab, pos, Quaternion.LookRotation(dir));
     }
 }

That's just an example.

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 Marshal_Alessi · Jan 18, 2017 at 04:25 AM 0
Share

Okay, I see. I was trying to do it a very backwards way by passing on the point of the mouseclick. Thank you for your help.

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

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

Not shooting in desired direction 1 Answer

2D Shooting right only. Doesn't flip direction with character 1 Answer

For some akward reason bullet is moving in the wrong direction 1 Answer

Enemy shoots player (Tower) 0 Answers

bullets fly to the wrong direction in specific situations 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