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 ExpiredIndexCard · Aug 24, 2013 at 08:17 PM · c#mousegunlookat

Gun Look at mouse script not working

I want my gun to look at my mouse like in this game:

http://www.youtube.com/watch?v=rAwIzZ49V7M

Here is my script:

 Vector3 gunLookPos = new Vector3(0f, Input.mousePosition.y, Input.mousePosition.x);
         Camera.main.ScreenToWorldPoint(gunLookPos);
         myTransform.LookAt(gunLookPos);
Comment
Add comment · Show 1
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 getyour411 · Aug 24, 2013 at 10:04 PM 1
Share

Do you have your X and Z values reversed in gunLookPos? Are you assigning your Camera.main... return value to something?

1 Reply

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

Answer by robertbu · Aug 25, 2013 at 12:58 AM

ScreenToWorldPoint() takes a Vector representing 'x', 'y' and 'z'. 'x', and 'y' come from the mouse, 'z' is the distance from a plane going through the camera to the plane the character is on. The code below calculates that distance dynamically, but for a 2D game, with the camera a fixed distance from the 2D surface, you can hard code that value. This code assumes the camera is looking towards positive 'z' like the default setup you get when you create a new scene:

 Vector3 gunLookPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, myTransform.position.z - Camera.main.transfrom.position.z);
 gunLookPos = Camera.main.ScreenToWorldPoint(gunLookPos);
 myTransform.LookAt(gunLookPos, Vector3.forward);
Comment
Add comment · Show 5 · 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 ExpiredIndexCard · Aug 25, 2013 at 06:41 PM 1
Share

$$anonymous$$y game is actually looking down the x - axis, just so that I can make my player move down his forward position. I tried this code:

 Vector3 gunLookPos = new Vector3(Input.mousePosition.x, myTransform.position.y - Camera.main.transform.position.y, Input.mousePosition.y);
         gunLookPos = Camera.main.ScreenToWorldPoint(gunLookPos);
         myTransform.LookAt(gunLookPos, Vector3.forward);

and this one:

 Vector3 gunLookPos = new Vector3(myTransform.position.x - Camera.main.transform.position.x, Input.mousePosition.x, Input.mousePosition.y);
         gunLookPos = Camera.main.ScreenToWorldPoint(gunLookPos);
         myTransform.LookAt(gunLookPos, Vector3.forward);

But they dont seem to work. Am I missing something?

avatar image ExpiredIndexCard · Sep 01, 2013 at 08:07 PM 1
Share

Can someone please help me? I really need help. I still cannot figure this out! I have tried everything. If you want, I will send you a build of the game or something.

avatar image robertbu · Sep 01, 2013 at 09:25 PM 2
Share

If you are looking down the 'x', then you calculate the distance for the 'z' parameter of he Vector3 as:

 myTransform.position.x - Camera.main.transfrom.position.x
avatar image MrProfessorTroll · Sep 01, 2013 at 10:15 PM 1
Share

I think the problem is that you were switching around the input.mousPosition. Also, robertbu is correct. It should work if you use that combined with his original answer. Good luck!

avatar image ExpiredIndexCard · Sep 01, 2013 at 10:16 PM 1
Share

It works actually, thanks. Yes $$anonymous$$rProfessorTroll, that was the problem. Thanks robertbu

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

16 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

Related Questions

Multiple Cars not working 1 Answer

How to check for a mouse drag 0 Answers

Distribute terrain in zones 3 Answers

Clamp a mouse around several 2d objects 1 Answer

Reload a gun in VR with motion controllers 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