Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by daviddickball · Aug 29, 2015 at 03:38 PM · raycastdirectionpointerarrowoffscreen

Arrows pointing to offscreen enemy

Hi guys, I'm looking for a good tutorial for how to create arrows that point in the direction of a target offscreen. The arrows would be 2D gui, pointing to 3D objects, and sit just near the edge of the screen.

alt text

Is there a tutorial for this, because I think the maths would make my head explode!

I think what I need to do is draw a line to the enemy, and then detect where it touches the edge of the screen, and place my arrow there. But I'm not sure how to do that without complicated mathematics!

Comment
Add comment · Show 2
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 azmundai · Aug 29, 2015 at 04:40 PM 0
Share

Try calculating the angle between the two 3d game objects positions, and then applying that angle to the 2d object? so something like:

float angle = Vector3.Angle(myShip.transform.position, otherShip.transform.position);

would give you the angle

Then something like :

arrow.transform.rotation.eulerAngles = new Vector 3(0, angle, 0);

Just use a fixed size arrow for now to see if you can get that working.

avatar image daviddickball · Aug 30, 2015 at 04:37 PM 0
Share

Thanks for the help @azmundai, but your calculation doesn't point the arrow towards the enemy. The angle never becomes a negative, so points mostly upwards.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Scribe · Aug 29, 2015 at 04:54 PM

You can use the builtin camera methods to get screen point and viewport point, both could be useful depending on the desired outcome, my example uses viewport as it tends to be easier to work with (as it's always between (0, 0) (bottom left) and (1, 1) (top right).

 Vector3 screenPos;
 Vector2 onScreenPos;
 float max;
 Camera camera;
 
 void Start(){
     camera = Camera.main;
 }
 
 void Update () {
     screenPos = camera.WorldToViewportPoint(transform.position); //get viewport positions
 
     if(screenPos.x >= 0 && screenPos.x <= 1 && screenPos.y >= 0 && screenPos.y <= 1){
         Debug.Log("already on screen, don't bother with the rest!");
         return;
     }
 
     onScreenPos = new Vector2(screenPos.x-0.5f, screenPos.y-0.5f)*2; //2D version, new mapping
     max = Mathf.Max(Mathf.Abs(onScreenPos.x), Mathf.Abs(onScreenPos.y)); //get largest offset
     onScreenPos = (onScreenPos/(max*2))+new Vector2(0.5f, 0.5f); //undo mapping
     Debug.Log(onScreenPos);
 }
Comment
Add comment · Show 4 · 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 daviddickball · Aug 30, 2015 at 04:39 PM 0
Share

Thanks for commenting @Scribe but I'm not sure how this helps. Surely what I need is an angle towards the player, and a location at the edge of the screen to place the arrow. I'm unsure what your mathematics is providing?

avatar image Scribe · Aug 30, 2015 at 09:53 PM 0
Share

The code I posted gets the point at the edge of the screen, which a line would intercept if drawn from the centre of the screen to the target, this does assume the player is always close to the centre of the screen.

avatar image Toknoy · Jul 03, 2019 at 08:33 AM 0
Share

oh thanks this helped me finding the anchors value should be.

avatar image aoboaten Toknoy · Aug 01, 2020 at 01:14 AM 0
Share

I am trying to use the script above. Should I just create a GameObject in a UI canvas, attach a cursor image and then attach this script?

avatar image
0

Answer by morvi · Aug 19, 2017 at 05:56 PM

Hello,

I just used the code above and it works perfectly fine to me. My problem is, I don't understand, why it works ...especially this line:

 onScreenPos = new Vector2(screenPos.x-0.5f, screenPos.y-0.5f)*2; //2D version, new mapping

I tried and tested and made some changes and it turned out it has to be done this way, but since screenPos can have any value, depending on where related to transform.position the viewport currntly is, I don't see how it can work.

I am really going crazy because of this...please help.

Thank you so much in advance

Morvi

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 Marrt · Oct 26, 2017 at 06:37 PM 2
Share

This line just transforms the representation of the Viewport-space. Normally values range from: Vector2(0F,0F) to Vector2(1F,1F)

The line "maps" it to: Vector2(-1F,-1F) to Vector2(1F,1F) So e.g. Vector2(0.5F,0.5F) becomes Vector2(0F,0F) and Vector2(0F,0F) becomes Vector2(-1F,-1F)

the max then finds out which of x and y is closer to the edge. It then scales up the vector so that that closer value exactly hits the edge. Afterwards he reverts the mapping to be Vector2(0F,0F) to Vector2(1F,1F) again

You can even make this work in 3D-Space if you invert+forceclamp if the z value is negative

 Vector3 viewportPos        = cam.WorldToViewportPoint( worldPosition );
 bool    behindCamera    = viewportPos.z < 0F;
 
 if(behindCamera){
     //invert vector and clamp
     viewportPos = Vector3.one-viewportPos;
     viewportPos = ClampToSquare(viewportPos); //above clamping code
 }


avatar image
0

Answer by Gargosian · Dec 14, 2020 at 01:21 PM

This also might help some people:

https://www.youtube.com/watch?v=dHzeHh-3bp4

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

34 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

Related Questions

Dragging object, raycast blocked by object 0 Answers

Quaternion and Direction for DrawRay(Make Object Look At Its Feet, Watch where its going...) 2 Answers

Problem with raycasting towards mouse 0 Answers

Check if player is facing an Object with Tag and set distance away 1 Answer

How can I incorporate a Rotation Towards the Mouse Position into this Script? I Tried. 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