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 /
This question was closed Dec 18, 2015 at 04:54 PM by Lepanto7 for the following reason:

Answered my own question.

avatar image
0
Question by Lepanto7 · Dec 13, 2015 at 02:57 PM · scripting problem

How to make a gun autoaim on targets under the reticule?

Hey all. I'm new to Unity, and C# too, so apologies if the answer is obvious.

Basically, I'm trying to make a gun that automatically targets and auto-aims at any GameObject tagged "Enemy" which passes under the player's aiming reticule (a GUIElement). Sort of like Star Wars: Battlefront II.

Here's my code so far:

        if (!target)
                             {
                                 if (gunCrosshairs.HitTest(Camera.main.ViewportToScreenPoint(playerTarget.transform.position)))
                                 { target = playerTarget; }
 
                                 else
                                 {
                                     GameObject inCrosshairs = transform.gameObject;
                                      }
 
                                 else if (gunCrosshairs.HitTest(Camera.main.ViewportToScreenPoint((GameObject.FindGameObjectWithTag("Enemy")).transform.position)))
                                 {
                                     GameObject inCrosshairs = transform.gameObject;
                                     if (inCrosshairs.tag == "Enemy")
                                     {
                                         target = inCrosshairs;
                                     }
                                 };
                             }
                         
                         if (!gunCrosshairs.HitTest(target.transform.position))
                             { target = null; }

Here's how it's supposed to work. If the variable "target" is null (i.e. there was no enemy under the autoaim cursor), the script checks for "Enemy" GameObjects under the targeting reticle. First, it checks for the player's manually selected target GameObject, "playerTarget". If "playerTarget" is not under the reticle (a simple boolean check using HitTest), the script then searches for the first "Enemy" that falls under the crosshair instead.

Once identified, the "Enemy" GameObject under the reticle will be stored in variable "target", which the autoaim will use to aim at that particular enemy. If no "Enemy"'s of any kind are under the autoaim reticle, it will clear the variable "target".

I've been trying to use HitTest to identify when an enemy is under the crosshair. Problem is, since HitTest returns a bool, and I need to store the "Enemy" GameObject to a variable, I don't know how to use HitTest to supply a variable. If that makes any sense.


Also, I'm having trouble with linking my crosshairs GUIElement to this script in the Inspector. Whenever I try to find that GUIElement and drag it to the Inspector, the Inspector doesn't recognize it, even though I added the following public variable to the main script:

public GUITexture gunCrosshairs;

Any idea why the Inspector might not be recognizing the GUIElement I'd already added to the canvas? What settings should I check?


Any help on both (or either) of these questions would be very helpful! :)

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make script affect only one this game object 2 Answers

error Int to string?Assets/Scripts/MyConnect.cs(61,60): error CS0029: Cannot implicitly convert type `int' to `string' 1 Answer

How can i limit object dragging area in my inventory? 0 Answers

Door open script opens all the doors in the scene 3 Answers

Make an object a certain distance from me. 2 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