• 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 Phobos56 · Sep 26, 2017 at 03:25 PM · sniper

Display distance in my sniper scope ?

In english

I make a FPS project on Unity.

I add a sniper and i would like see the distance of my ennemi when i use the scope.

Do you know any script for that ?

Thank you

En français

Je suis sur un project de FPS

J'ai ajouté un sniper et je voudrais afficher la distance de mon ennemi quand je zoom dessus.

Connaissez vous un script qui le permettrais?

Merci

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
0
Best Answer

Answer by Hellium · Sep 26, 2017 at 04:40 PM

English

I would advise to use Raycasting

 public new Camera camera ; // Drag & drop the camera of your scene here
 
 private void Start()
 {
     // The following lines make sure there is a camera if none has been specified
     if( camera == null )
         camera = Camera.main ;
     if( camera == null )
         camera = FindObjectOfType<Camera>();
 }
 
 public float GetDistanceFromTarget()
 {
     // Cast a ray from the center of the screen
     Ray ray = camera.ViewportPointToRay( new Vector3(0.5f, 0.5f, 0) );
     RaycastHit $$anonymous$$t;
     if (Physics.Raycast(ray, out $$anonymous$$t))
         return $$anonymous$$t.distance ;
     else
         return Mathf.Infinity;
     // or shorter version :
     // return Physics.Raycast(ray, out $$anonymous$$t) ? $$anonymous$$t.distance : Mathf.Infinity ;
 }

If you want t$$anonymous$$s method to work, your ennemies and the elements in your scene must have a collider.


Français

L'utilisation de la technique de Raycasting me semble la plus appropriée

 public new Camera camera ; // Glisser et déposer la caméra de la scène dans l'inspecteur
 
 private void Start()
 {
     // Les lignes suivantes permettent de récupérer une caméra dans la scène si aucune n'a été spécifiée
     if( camera == null )
         camera = Camera.main ;
     if( camera == null )
         camera = FindObjectOfType<Camera>();
 }
 
 public float GetDistanceFromTarget()
 {
     // Lancer de rayon depuis le centre de l'écran
     Ray ray = camera.ViewportPointToRay( new Vector3(0.5f, 0.5f, 0) );
     RaycastHit $$anonymous$$t;
     if (Physics.Raycast(ray, out $$anonymous$$t))
         return $$anonymous$$t.distance ;
     else
         return Mathf.Infinity;
     // Version plus concise :
     // return Physics.Raycast(ray, out $$anonymous$$t) ? $$anonymous$$t.distance : Mathf.Infinity ;
 }


Évidemment, pour que cette technique marche, il sera nécessaire que les ennemis (et les éléments de décor) soient munis d'une boite de collision.

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 Phobos56 · Sep 27, 2017 at 10:13 AM 0
Share
avatar image
-1

Answer by connorwforman · Sep 26, 2017 at 04:23 PM

Are you trying to zoom in on the enemy? If so, you would do somet$$anonymous$$ng like t$$anonymous$$s.

 public Camera playerCamera; 
 private bool isZoomed = false; 
 
 void Start() {
 playerCam camera = GetComponent<fieldOfView>(); 
 }
 
 void Update () {
        if (Input.GetButtonDown("FIre2")) {
                 isZoomed = !isZoomed; 
                 playerCam = 40; 
                 
        }
 }



If t$$anonymous$$s does not work try doing the "Snipe scope effect" video on YouTube by brackeys.

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 Hellium · Sep 26, 2017 at 04:31 PM 0
Share

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

70 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sniper bullets not working 0 Answers

A sniper rifle tutorial 1 Answer

How to make my gun semi-auto? 1 Answer

How to add sniper zoom effect in an FPS made with Unity3d? 2 Answers

raycast hit point is wrong 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