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 /
This question was closed Nov 06, 2012 at 04:17 PM by Griffo for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Griffo · Nov 05, 2012 at 08:01 PM · guitargettrackingradar

Track target

Hi, can someone point me in the right direction please, I'm looking to write a script to track an incoming target across the screen with a GUI texture, like in my picture below, and when the target goes outside the camera view the GUI texture will track the screen edge until the game object it's tracking is destroyed.

So any ideas of the best way to go about this would be appreciated.

Thanks.

alt text

grab-01.jpg (77.6 kB)
Comment
Add comment · Show 4
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 3D-Magic-VR · Nov 05, 2012 at 08:06 PM 0
Share

Do you have any code snippet?, because if you need help with this you need at least show some part of your code, the tracking part, so any one who see this can have an idea what are you talking about. :-)

avatar image Griffo · Nov 05, 2012 at 08:10 PM 0
Share

No, not started to write it yet, was looking for some pointers so I don't waste a lot of time going down the wrong road.

avatar image Scribe · Nov 05, 2012 at 08:30 PM 1
Share

take a look at

Camera.WorldToScreenPoint

http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html

Also you will probably nee

$$anonymous$$athf.Clamp

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html

for keeping the GUI on screen.

Hope thats useful, Scribe

avatar image Griffo · Nov 05, 2012 at 08:39 PM 0
Share

Scribe, thanks, that could be just what I need.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Griffo · Nov 06, 2012 at 04:17 PM

Ok .. Sorted, needed to track GameObject(Clone) not just the GameObject

Comment
Add comment · 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
0

Answer by Griffo · Nov 06, 2012 at 03:37 PM

Ok that was easier than I thought it would be, but I have another problem, if I drop a game object onto target in the inspector it tracks it as expected, but if I drop a prefab onto target in the inspector when the prefab is initiated it does not track it, why?

The first script is the tracking script, the second is throw Grenade script, In the throw grenade script it turns on the Track Grenade game object so the tracking script will run, then just before destroying the Grenade game object turns it off again.

Thanks.

 #pragma strict
 
 var target : Transform;
 var trackIcon : Texture2D;
 
 function Awake(){
 
 }
 
 function Start(){
 
 }
 
 function Update () {

     var screenPos : Vector3 = Camera.main.WorldToScreenPoint (target.position);
 }
 
 function OnGUI(){
 
     var screenPos : Vector3 = Camera.main.WorldToScreenPoint (target.position);
     var reverse : float = Screen.height;
     var clampX = Mathf.Clamp(screenPos.x, 0, Screen.width - (trackIcon.width / 2));
     var clampY = Mathf.Clamp(reverse - screenPos.y, 0, Screen.height - (trackIcon.height / 2));
     
     GUI.DrawTexture(Rect(clampX, clampY, trackIcon.width / 2, trackIcon.height / 2), trackIcon);
 }


 #pragma strict
 
 var grenadeExplosion : Transform;
 var Explosion : Transform;
 var explosionSound : AudioClip;
 var hitSound : AudioClip;
 var trackGrenade : GameObject;    // Referance to the GameObject with the TrackTarget on
 
 private var creationTime : float;
 private var loop : boolean = true;
 private var boxCollider : BoxCollider;
 private var playerDamage : GameObject;
 
 function Awake(){
 
     creationTime = Time.time;
 }
 
 function Start(){
 
     trackGrenade = GameObject.Find("Track Grenade");
     playerDamage = GameObject.Find("Main Camera");
     trackGrenade.gameObject.SetActive(true);
     boxCollider = transform.GetComponent(BoxCollider) as BoxCollider;
 }
 
 function Update(){
 
     if(Time.time > (creationTime + 4)){
         destroyGrenade();
     }
 }
 
 // Apply damage to the Player acording to where they are in the blast range
 function AreaDamageEnemies(location : Vector3, radius : float , damage : float ){
 
     var objectsInRange : Collider[] = Physics.OverlapSphere(location, radius);
     
     for (var col : Collider in objectsInRange){
         var player : FirstPersonControl = col.GetComponent(FirstPersonControl);
         if (player != null)
         {
 // linear falloff of effect
             var proximity : float = (location - player.transform.position).magnitude;
             var effect : float = 1 - (proximity / radius);
             
 // Apply damage to the Player in the PlayerDamage.js on the Main Camera game object (Player)
             playerDamage.GetComponent(PlayerDamage).ApplyDamage(damage * effect);
         }
     }
 }
 
 function OnCollisionEnter(collision : Collision) {
 
     audio.volume = 1.0;
     audio.PlayOneShot(hitSound);
     }
 
 function destroyGrenade(){
     
 if(loop == true){
     loop = false;                                    // Set loop to false so this destroy() function dont keep repeating
     AreaDamageEnemies(transform.position, 10, 10);    // Set the blast radius of the Grenade and call AreaDamageEnemies function above
     boxCollider.size = Vector3(0,0,0);                // Resize the Box Collider down to 0 so you dont get an error with the
                                                     // RayCast trying to access it after the Grenade has been destroyed
     Instantiate(grenadeExplosion, transform.position + Vector3(0, 0, 0), Quaternion.identity);
     renderer.enabled = false;                        // Turn off the mesh render so you dont see the Grenade while the explosion effect and explosion sound sample play out
     Instantiate(Explosion, transform.position + Vector3(0, 0.7, 0), transform.rotation);
     audio.volume = 2.0;
     audio.PlayOneShot(explosionSound);
     yield WaitForSeconds (3.0);
     trackGrenade.gameObject.SetActive(false);
     Destroy (gameObject);                             // Destroy the Grenade
     }
 }
         
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

12 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

Related Questions

'Mission Complete/Game Over' screen once objective is complete. 1 Answer

Hitmarker appears on both targets 2 Answers

multipe targets 1 Answer

Why is it that when i click on a gui element it sends a mousedown and mouseup to the LAST gameobject that i clicked? 0 Answers

Cut Gui rect 0 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