• 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 orv · Mar 04, 2010 at 05:47 PM · vector3lookatvector

Make an FPS gun point at the mouse position.

Hi,

Im new to unity and trying to make an on rails shooter, similar to time crysis or house of the dead. I also want the gun to be displayed on the screen but to point at where the mouse is, similar to how it does in red steel on the wii.

My problem is that I cant get the gun to reliability point at the cursor.

I found some usefull code HERE

private var targetPlane : Plane;

function Start(){ targetPlane = Plane(Vector3().up, 0); }

function Update () {

var ray = Camera.main.ScreenPointToRay(Input.mousePosition); var dist : float; if(targetPlane.Raycast(ray, dist)){ var pos = ray.GetPoint(dist); transform.LookAt(pos, Vector3().up); } }

This works well, except you cant point the gun up. So i changed Vector3().up to Vector3.().one. Now the gun can point in all directions, untill the camera faces back on itself from the starting position, when the gun will no longer follow the mouse at all.

Im using some code that I found HERE to move the camera from point to point, if its any help.

Ive been surching through the unity forums and pulling my hair out for best part of two days now. All Ive managed to figgure out is that Im 99% sure its something to do with where the plane is drawn and not being able to see it when the camra is rotated.

Thanks in advance!

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

2 Replies

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

Answer by Eric5h5 · Mar 04, 2010 at 08:39 PM

Try using ScreenToWorldPoint:

var zDistance = 10.0;

function Update () { var mousePos = Input.mousePosition; transform.LookAt(Camera.main.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, zDistance))); }

Comment
Add comment · Show 3 · 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 orv · Mar 05, 2010 at 11:53 AM 0
Share

Yep, that it it. Thanks very much!

avatar image jeanRicK · Dec 21, 2010 at 09:48 PM 0
Share

Quick question... I am trying to use this method to achieve something similar (if not the same thing). Basically, I'm trying to get a gun to point at the cursor, however, when I use this method it feels like the gun isn't pointing at the cursor directly all the time especially as it moves from the 'center' of the screen. It feels like it has to be scaled to fit the screen or something. Is this normal and if so how do you fix this?

avatar image Eric5h5 · Dec 21, 2010 at 10:52 PM 0
Share

@jeanRic$$anonymous$$: try setting zDistance to different values.

avatar image
0

Answer by caden-burleson · Jan 31, 2017 at 08:26 PM

What about if It's a third person character an I have the gun parented to the character. It seems that the guns rotation starts off in the wrong rotation and isn't following the target properly....

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Vector3 Projection 2 Answers

How do I get one object to point toward another using rigidbody physics? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Checking if a Vector3 is a int 0 Answers

[HELP] Translating vector 2 co-ordinates to real space 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