Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Major · Oct 25, 2012 at 03:22 AM · vector3distancedebugturretlook

Mesuring distance through script

I have a turret script that in its previous version would use a trigger to judge if it was to shoot at an enemy. That worked okay, but enemies would have to enter the trigger one by one. I want it so that it uses a script to decide if an enemy is in range. I believe that this will solve my issue. Thanks!

  var distance;
     var Look : boolean = false;
     var Target : Transform;
     var damp : float;
     var Projectile : Transform;
     var Shell : Transform;
     var MuzzleFlash : Transform;
     var fireRate : float =  5;
     var Speed : int = 2000;
     var ShellSpeed : int = 500;
     var TurretAudio : AudioClip; 
     var TurretAnimation : AnimationClip;
     static var ammo : int = 250;
     var AMMO : int;
     var AttackDistance = 10.0;
     
     private var nextFire = .5;
     
     function Update()
     {
         distance = Vector3.Distance(Target.position, transform.position);
         
         if(distance < AttackDistance)
         {
             if(gameObject.tag == "Enemy")
             {
                 Look = true;
                 if(!Target)
                 {
                     Target = gameObject.transform;
                 }
             }
          
         if(Look && Target != null)
         {
             var rotate = Quaternion.LookRotation(Target.position - transform.position);
             
             transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
             Shoot();
         }
         
         AMMO = ammo;
         }
     }
     
     function Shoot()
     {
         if(ammo > 0)
         {
         if(Time.time > nextFire)
         {
             nextFire = Time.time + fireRate;
                 
                 var muzzleflash = Instantiate(MuzzleFlash, transform.Find("Spawn").transform.position, transform.rotation);
                 
                 var projectile = Instantiate(Projectile, transform.Find("Spawn").transform.position, transform.rotation);    
                 projectile.rigidbody.AddForce(transform.forward * Speed); 
                 
                 var shell = Instantiate(Shell, transform.Find("Spawn_2").transform.position, transform.rotation);
                 shell.rigidbody.AddForce(transform.right * ShellSpeed);
                 
                 audio.PlayOneShot(TurretAudio);
                 animation.Play(TurretAnimation.name);
             ammo --;
         }
     }
     }
 
 
 
 

REVSIDED CODE:

  var Look : boolean &#61; false;
     var Target : Transform;
     var damp : float;
     var Projectile : Transform;
     var Shell : Transform;
     var MuzzleFlash : Transform;
     var fireRate : float =  5;
     var Speed : int = 2000;
     var ShellSpeed : int = 500;
     var TurretAudio : AudioClip; 
     var TurretAnimation : AnimationClip;
     static var ammo : int = 250;
     var AMMO : int;
     var ViewDistance = 10.0;
     var AttackDistance = 8.0;
     
     private var nextFire = .5;
     
     function Update()
     { 
         distance = Vector3.Distance(Target.position, transform.position);
         if(distance < ViewDistance)
         { 
         
         if(distance < AttackDistance)
         { 
         
         if(gameObject.tag == "Enemy")
         { 
             Look = true; 
             if(!Target) 
             {
                 Target = gameObject.transform; 
             } 
         }
     
         if(Look && Target != null)
         { 
             
             var rotate = Quaternion.LookRotation(Target.position - transform.position);        
     
             transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
         
             Shoot(); 
         }
     
                 AMMO = ammo; 
             } 
         } 
     }
     
     function Shoot()
     {
         if(ammo > 0)
         {
         if(Time.time > nextFire)
         {
             nextFire = Time.time + fireRate;
                 
                 var muzzleflash = Instantiate(MuzzleFlash, transform.Find("Spawn").transform.position, transform.rotation);
                 
                 var projectile = Instantiate(Projectile, transform.Find("Spawn").transform.position, transform.rotation);    
                 projectile.rigidbody.AddForce(transform.forward * Speed); 
                 
                 var shell = Instantiate(Shell, transform.Find("Spawn_2").transform.position, transform.rotation);
                 shell.rigidbody.AddForce(transform.right * ShellSpeed);
                 
                 audio.PlayOneShot(TurretAudio);
                 animation.Play(TurretAnimation.name);
             ammo --;
         }
     }
     }
      
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 Major · Oct 28, 2012 at 03:34 PM 0
Share

Hello?!?!

2 Replies

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

Answer by Montraydavis · Oct 31, 2012 at 02:16 PM

To get distance, do this...

 var TurretDistance : float = (transform.position - Target.transform.position).sqrMagnitude * (transform.position - Target.transform.position).sqrMagnitude ;
Comment
Add comment · Show 2 · 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 Major · Oct 31, 2012 at 02:19 PM 0
Share

Thanks for replying so fast. I will try that when I can get to my computer. Thanks!

avatar image Montraydavis · Oct 31, 2012 at 02:21 PM 0
Share

You are welcome :) Feel free to mark as answered, or vote up if this ends up working for you. Thanks for using Unity :)

avatar image
1

Answer by Tim-Michels · Oct 31, 2012 at 02:34 PM

Well if you simply want the distance between 2 points, there's a simpler option:

float distance = Vector3.Distance(transform.position, Target.transform.position);

This should be the simplest and quickest way of getting the distance.

Cheers ;)

Comment
Add comment · 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 Major · Oct 31, 2012 at 03:05 PM 0
Share

I also need to limit how far the turrets can see. How would I integrate that?

avatar image Tim-Michels · Oct 31, 2012 at 03:19 PM 0
Share

You could add a member ViewDistance and only perform further checks for attacking if the distance is smaller than ViewDistance.

I hope that covers your question...

avatar image Major · Nov 07, 2012 at 02:49 PM 0
Share

Can you give me an example? I am new in this area.

avatar image Tim-Michels · Nov 07, 2012 at 03:11 PM 0
Share

Well, first you add a new var in your script: var ViewDistance = 10.0;

Then you Update function should look something like this:

function Update() { distance = Vector3.Distance(Target.position, transform.position); //Calculate distance every frame

if(distance < ViewDistance) //Check if distance is smaller than maximum view distance { //Only perform further checks when the target is in viewing range. //If the target is further than the ViewDistance, its not needed to calculate further stuff if(distance < AttackDistance) { if(gameObject.tag == "Enemy") { Look = true; if(!Target) { Target = gameObject.transform; } }

if(Look && Target != null) { var rotate = Quaternion.LookRotation(Target.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); Shoot(); }

A$$anonymous$$$$anonymous$$O = ammo; } } }

Sorry for the bad code formatting, just copy paste it in your script, code doesn't look good in a comment

thumbs up ;)

avatar image Major · Nov 16, 2012 at 12:19 AM 0
Share

Okay so I have posted the code that I used from you comment and integrated it into my previous code. Although all it is doing is creating a giant lag machine and won't target anything.

Show more comments

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

11 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

Related Questions

Enemy Jitters when i want him to stop a certain distance 0 Answers

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer

Are Unity distances that small? 1 Answer

Angle of 2 Vector3 on one axis 3 Answers

Raycasting using local space instead of global 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