Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Fetdressing · May 13, 2013 at 01:07 AM · collidershootingontriggerstay

Tower, get target within range by mouseclick

Hello! I am working on a tower shooter script where I have made the tower shoot at random targets within a certain range. I am now trying to make it so that when I ctrl-click an enemy I wan't the tower within range to start firing at this target. It does already shoot at my target but they can shoot across the whole map. I need help on limiting the range of the target-shooting. Here is what I've got so far:

 private var target;
 private var posTarget : Vector3;
 public var projectile : Rigidbody;
 private var cloneProjectile;
 public var projectileSpeed : float = 900;
 public var skottEld : GameObject;
 private var cloneSkottEld;
 private var center : Vector3;
 
 
 
 
 //private var getParent : GameObject;
 
 private var nextShot = 0.0;
 var reloadTime = 0.5;
 
 
 
 
 function Update () { 
     
 //Controll attacks
 if(Input.GetKey(KeyCode.LeftControl)){
     if(Input.GetKeyDown(KeyCode.Mouse0)){
         var hitTower : RaycastHit;
             
             if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hitTower)){
             
             
             
             if(hitTower.collider.tag == "Enemy"){
                 target = hitTower.collider.gameObject;
                 }
                 
                 
             }
             
     }
 }
 
 gameOver = HPTower.gameOver;
 if(gameOver == true){
     Destroy(this.transform.root.gameObject);
 }
 
 
 if(target == null){
     target = transform.root.GetComponent(Detector).target;
 }
 
 //target = Detector.target();
     //target = GameObject.FindGameObjectWithTag("GUISHOP").GetComponent(Buy).DMGUpgrade;
     if(target != null){
     
         posTarget = Vector3(target.transform.position.x,target.transform.position.y,target.transform.position.z);
         this.transform.LookAt(posTarget);
     if(Time.time > nextShot){
         nextShot = Time.time + reloadTime;
     
         
         
         
         
         
         
         
         cloneProjectile = Instantiate(projectile, transform.position, transform.rotation);
         
         
         cloneProjectile.SendMessage("getTarget", target);
         //cloneProjectile.rigidbody.AddForce(cloneProjectile.transform.forward * projectileSpeed); 
         Destroy(cloneProjectile.gameObject, 6);
         
         //cloneSkottEld = Instantiate(skottEld, transform.position, transform.rotation);
         //Destroy(cloneSkottEld, 3);
         
         
     }
     }
     
     
 
         
 }

This is the script that get the target position and then starts firing at it. And this script detects enemies in the range and then sends a target to the firing script:

 public var target : GameObject;
 
 
 function OnTriggerStay(hitTarget : Collider){
 
     if(hitTarget.gameObject.tag == "Enemy" && target == null){
         target = hitTarget.gameObject;
         
 
     }
     
 }

Thanks in advance! //Simon

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

1 Reply

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

Answer by greenshadow · May 13, 2013 at 02:30 AM

I'm having some difficulty deciphering your logic in the script, but something like this should do the trick. Make a float variable called range, specifying the range you want the tower to have. After you cast the ray to see if a target was clicked:

 float distance = Vector3.Distance(myTowerPosition, myIntendedTargetPosition);
 if (distance <= range) {
     //set the target, fire projectile, etc
 }
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 Fetdressing · May 13, 2013 at 10:00 AM 0
Share

Thanks! Yeah, that should work but do you know how I can retriew sphere collider radius? I am using a sphere collider as detector so then I somehow need to get the radius of it, any ideas?

avatar image Fetdressing · May 13, 2013 at 02:24 PM 0
Share

Ok, I used sphereRadius = this.gameObject.GetComponent(SphereCollider).radius;

And it works just fine. You just have to remember that collider sometimes scale different on objects.

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

15 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

Related Questions

OnTriggerStay Question 0 Answers

OnTrigger event when colliders are already touching eachother 1 Answer

Trigger animation going crazy 3 Answers

How can I get my script to recognise when a collider exits while the box collider of the trigger object is disabled? 1 Answer

I'm having a problem with OnTriggerStay2D 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