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 Griffo · Jul 20, 2013 at 03:59 PM · lerpclampmathf.clampmathf.lerp

Tracking a projectile with Mathf.Clamp

Hi, I'm using this script to track an incoming projectile, when the projectile starts to come at the player a trackIcon lerps and locks onto the projectile, and if the projectile falls onto or in front of the player it all looks and works fine but if the projectile goes over the player and lands then the trackIcon then come up from the bottom of the screen just before the projectile hits the ground, as in the picture I've added below, can someone please tell me how to stop the trackIcon coming up from the bottom, and just clamp it at the bottom of the screen if the projectile goes over the player, thanks.

 #pragma strict
 
 var trackIcon : Texture2D;
 private var timeRemaining : float;
 
 function Update () {
 
     if(Camera.main){
         timeRemaining += Time.deltaTime;
     }
 }
 
 function OnGUI(){
 
     if(Camera.main){
         var target : Transform = transform.parent;
         var screenPos : Vector3 = Camera.main.WorldToScreenPoint (target.transform.position);
         var reverse : float = Screen.height;
         var clampX = Mathf.Clamp(screenPos.x - 16, 0, Screen.width - trackIcon.width / 2);
         var clampY = Mathf.Clamp(reverse - screenPos.y - 16, 0, Screen.height - (trackIcon.height / 2));
     
         GUI.DrawTexture(Rect(clampX, clampY, Mathf.Lerp(100, 32, timeRemaining), Mathf.Lerp(100, 32, timeRemaining)), trackIcon);
     }
 }

alt text

untitled-2.jpg (32.6 kB)
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 Griffo · Jul 20, 2013 at 07:22 PM

I've gone another way and edited a wiki script to suit my needs .. here it is if anyone else ever needs it ..

 #pragma strict
 
 var target : Transform;                // Object that this label should follow
 var clampToScreen = true;            // If true, label will be visible even if object is off screen
 var clampBorderSize = 0.0;            // How much viewport space to leave at the borders when a label is being clamped
 var useMainCamera = true;            // Use the camera tagged MainCamera
 var cameraToUse : Camera;            // Only use this if useMainCamera is false
 var timeToResizeIcon : float;        // Time taken to resize the tracking Icon
 
 private var cam : Camera;
 private var thisTransform : Transform;
 private var camTransform : Transform;
 private var originalTime : float;
 private var loop : boolean;
 
 function Start () {
 
     target = transform.parent;
 
     thisTransform = transform;
     if (useMainCamera)
         cam = Camera.main;
     else
         cam = cameraToUse;
     camTransform = cam.transform;
 
 // Rescale the size of the tracking icon down
     thisTransform.localScale -= Vector3(1, 1, 1);
 }
  
 function Update () {
 
     if(!loop){
         StartCoroutine(LerpScale(timeToResizeIcon));
     }
 
     if (clampToScreen) {
         var relativePosition = camTransform.InverseTransformPoint(target.position);
         relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
         thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition));
         thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
                                          Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
                                          thisTransform.position.z);
     }
     else {
         thisTransform.position = cam.WorldToViewportPoint(target.position);
     }
 }
 
 function LerpScale(time : float){
 
     loop = true;
     originalTime = time;
 
     while (time > 0.0f){
 
         time -= Time.deltaTime;
 
         guiTexture.pixelInset = Rect (Mathf.Lerp(-16, -32, time / originalTime), Mathf.Lerp(-16, -32, time / originalTime), 
         Mathf.Lerp(32, 64, time / originalTime), Mathf.Lerp(32, 64, time / originalTime));
 
         yield;
     }
 }
  
 @script RequireComponent(GUITexture)

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

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

my MainChara shakes when the Mathf.Clamp stops it from going off screen 1 Answer

Mathf.Clamp() jumping back to max value after reaching min value. 1 Answer

Camera Zoom script stops ability to pan 1 Answer

How can i Limit the movement of character (x, y axis) on camera view 0 Answers

How do I use mathf.clamp to make boundaries for a camera 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