Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 MattDahEpic · Jul 30, 2017 at 02:02 AM · raycastingfrustrum

Unable to place gameobject at edge of screen

I'm trying to make an arrow that points towards an objective offscreen, but have been running into placement problems. I have the rotation figured out, the objects point towards their intended targets just fine, but they get places way off screen. They should be right near the edge.

This game is in 2D.

My current code is: (currentDisplays is a dictionary with Transform as the value)

 Plane[] planes = GeometryUtility.CalculateFrustumPlanes(Camera.main); //edges of viewport
 Ray r = new Ray(Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, -10)), p.transform.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, -10))); //ray from center of screen to point
 float distToEdge;
 Vector3 displayPos = new Vector3(1000,1000,1000);
 //where does the ray collide with the edges of the viewport?
 if (planes[0].Raycast(r,out distToEdge)) { //left side of screen
     displayPos = r.GetPoint(distToEdge - 0.75f);
 }
 if (planes[1].Raycast(r,out distToEdge)) { //right side of screen
     displayPos = r.GetPoint(distToEdge - 0.75f);
 }
 if (planes[2].Raycast(r,out distToEdge)) { //bottom side of screen
     displayPos = r.GetPoint(distToEdge - 0.75f);
 }
 if (planes[3].Raycast(r,out distToEdge)) { //top side of screen
     displayPos = r.GetPoint(distToEdge - 0.75f);
 }
 
 currentDisplays[p].position = displayPos;
 Vector2 targetLook = p.transform.position - currentDisplays[p].transform.position;
 var angle = Mathf.Atan2(targetLook.y, targetLook.x) * Mathf.Rad2Deg;
 angle -= 90f;
 currentDisplays[p].rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Thank you for your help!

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

Answer by iBicha · Jul 30, 2017 at 02:14 AM

Hello MattDahEpic,

Please refer to this post, It seems like it is exactly what you are looking for, and actually might be cheaper on the CPU since it doesn't require Raycasting.

Cheers

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 MattDahEpic · Jul 30, 2017 at 03:43 AM 0
Share

I'm afraid that doesn't quite work as this game is 2D.

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

73 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with rotating my collider and ray origins 0 Answers

Best way to handle melle attacks in a fast paced 2D game 0 Answers

Raycast ignore layers except 1 Answer

FOV that brings gameObjects close 2 Answers

Is it possible to raycast multiple objects of the same tag (Enemy) 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