• 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
Question by MikeJ · Mar 12, 2010 at 09:40 PM · fpsgunshootaimaiming

Aiming Gun at Cursor

I know that there are some posts out there that kind-of cover t$$anonymous$$s, but I'm new to Unity and I'm working on a FPS prototype trying to figure out how to have the gun always point to the cursor in the center of the screen.

There are a few good scripts out there, however I do not know where to place them, or if they are necessarily the best t$$anonymous$$ng as far as performance is concerned (using Ray casting and getting the distance of the nearest object at the position of the cross-hairs)

There are two scripts I have found in the forums below. W$$anonymous$$ch is better to use for performance, and where do I actually place the script?, on What GameObject?

var gun : GameObject = (the gun);
var target : Vector3;
var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0)); // T$$anonymous$$s is assuming your crosshair is in the middle of the screen
var $$anonymous$$t : RaycastHit;
if (Physics.Raycast (ray, $$anonymous$$t)) {
 target = $$anonymous$$t.point; // We $$anonymous$$t somet$$anonymous$$ng, aim at that
} else {
 target = ray.GetPoint(100); // Distance we're aiming at, could be somet$$anonymous$$ng else
}
gun.transform.LookAt($$anonymous$$t.point);

And T$$anonymous$$s one:

var $$anonymous$$t : RaycastHit; var Gun : GameObject; var range = 100.0; var crosshairTexture: Texture2D; var position : Rect;

function Start ()

{ position = Rect ((Screen.width - crosshairTexture.width)/2, (Screen.$$anonymous$$ght - crosshairTexture.$$anonymous$$ght)/2 crosshairTexture.width, crosshairTecture.$$anonymous$$ght); }

function OnGUI () { GUI.DrawTexture (position, crosshairTexture); }

function Update ()

 {
  var ray = camera.ViewportPointToRay       
  (Vector3(0.5,0.5,0));
  Debug.DrawRay (ray.origin, ray.direction *
  range, Color.green);

  var $$anonymous$$t : RaycastHit;



  if (Physics.Raycast (ray, $$anonymous$$t))
      {Gun.transform.LookAt($$anonymous$$t.point);}
  else
      {Gun.transform.LookAt(GetPoint(range));}
 } 

Would the best solution be to translate a point in the screen to a world point, and then parent the gun's aim and cross-hair element to the mouse's position? Any help would be amazing, thank you so much!

Comment

People who like this

0 Show 0
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

Answer by Jason_DB · Mar 12, 2010 at 10:03 PM

One important question is what kind of FPS you are making, since you seem to be kind of describing a duck-hunt type game in w$$anonymous$$ch the camera has a fixed position (w$$anonymous$$ch would be true if you were to have the crosshair and aim dependent on the mouse position), even though both of the scripts you have fire the gun directly to the middle of the screen.

If you want to do a traditional FPS in w$$anonymous$$ch the camera moves with the mouse I would suggest making the crosshair and aiming dependent on the main camera (always being at the middle of the screen), and then having your gun object be a c$$anonymous$$ld of the main camera (so that it will move with it). Using the ScreenPointToRay would work for that, you would just need to get a hold of the camera (for instance through a findbytag):

    camera = GameObject.FindWithTag("MainCamera");

Also I wouldn't worry about the performance effects of the raycasts in t$$anonymous$$s case.

Comment
Mike 3
23cool1234
PwnedByACheeseburger

People who like this

4 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 MikeJ · Mar 12, 2010 at 10:44 PM 0
Share

Yeah but I'm having problems figuring out where to actually attach the script to, the camera? the cross-hair gui texture? the gun???

avatar image Jason_DB · Mar 12, 2010 at 11:09 PM 0
Share

I would make a new GameObject a child of the camera and then put the gun script on that. You can put the crosshair anywhere as long as you draw it in the centre of the screen, so it might be easiest to put it in the gun script.

avatar image MikeJ · Mar 12, 2010 at 11:22 PM 0
Share

I have a gun as a child of the Main Camera, and I have on that gun a script which controls how it shoots. How do I aim that gun toward the center no matter how close I am to the object. I tried casting the ray directly from the camera instead of the gun, but it still veers off to the right when I get close to the object. I can send you my scene if that helps

avatar image Jason_DB · Mar 12, 2010 at 11:36 PM 0
Share

OK, send it to info@dastardlybanana.com and I'll take a look at it.

avatar image MikeJ · Mar 12, 2010 at 11:48 PM 0
Share

Thanks, I sent it off. BTW I saw your video on your site. Very cool, I hope to eventually be to that point.

Show more comments
avatar image

Answer by MP0732 · Nov 08, 2012 at 02:22 AM

Jason_DB wrote,

I would make a new GameObject a c$$anonymous$$ld of the camera and then put the gun script on that. You can put the crosshair anywhere as long as you draw it in the centre of the screen, so it might be easiest to put it in the gun script.

I have no idea why that worked, but it worked like a charm. Earlier, I had tried to put the script on an empty c$$anonymous$$ld of the camera and then use that c$$anonymous$$ld as the parent of the weapon. That caused the center of the weapon to rotate around the center of the screen. Jason's way caused the weapon to remain fixed on the center of the screen, moving the weapon in a fixed point along with it.

Thanks, Jason. You've saved me a few headaches.

Comment

People who like this

0 Show 0 · 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

2 People are following this question.

avatar image avatar image

Related Questions

Aiming down a gun 4 Answers

Fps Controller fixed object (Gun) (Solved) 1 Answer

Aim to crosshair? 3 Answers

Weapon random movement 0 Answers

How do you do this :( 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