Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 2021 Cookbook

You're reading from   Unity 2021 Cookbook Over 140 recipes to take your Unity game development skills to the next level

Arrow left icon
Product type Paperback
Published in Sep 2021
Publisher Packt
ISBN-13 9781839217616
Length 816 pages
Edition 4th Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Shaun Ferns Shaun Ferns
Author Profile Icon Shaun Ferns
Shaun Ferns
Matt Smith Matt Smith
Author Profile Icon Matt Smith
Matt Smith
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

1. Displaying Data with Core UI Elements 2. Responding to User Events for Interactive UIs FREE CHAPTER 3. Inventory and Advanced UIs 4. Playing and Manipulating Sounds 5. Creating 3D Objects, Terrains, Textures, and Materials 6. 2D Animation and Physics 7. Characters, Game Kits, and Starter Assets 8. Web Server Communication and Online Version Control 9. Controlling and Choosing Positions 10. Navigation Meshes and Agents 11. Cameras and Rendering Pipelines 12. Shader Graphs and Video Players 13. Advanced Topics - Gizmos, Automated Testing, and More 14. Particle Systems and Other Visual Effects 15. Virtual and Augmented Reality (VR/AR)

The CalculateBlipPosition(...) method

First, we calculate angleToTarget, which is the angle from (0, 0, 0) to our normalized target position.

Next, we calculate anglePlayer, which is the angle the player's character is facing. This recipe makes use of the yaw angle of the rotation, which is the rotation about the Y-axis; that is, the direction that a character controller is facing. This can be found in the Y component of a GameObject's eulerAngles component of its transform. You can imagine looking from above and down at the character controller and seeing what direction they are facing  this is what we are trying to display graphically with the radar.

Our desired radar angle (the angleRadarDegrees variable) is calculated by subtracting the player's direction angle from the angle between the target and player, since a radar displays the relative angle from the direction that the player is facing to the target object. In mathematics, an angle of zero indicates an east direction. To correct this, we need to also subtract 90 degrees from the angle.

The angle is then converted into radians since this is required for these Unity trigonometry methods. We then multiply the Sin() and Cos() results by our normalized distances to calculate the X and Y values, respectively (see the following diagram):

Figure 2.37 – Calculation for the blip method
In the preceding diagram, alpha is the angle between the player and target object, "a" is the adjacent side, "h" is the hypotenuse, and "o" is the side opposite the angle.

Our final position values need to be expressed as pixel lengths, relative to the center of the radar. So, we multiply our blipX and blipY values by half the width and the height of the radar; note that we only multiply with half the width since these values are relative to the center of the radar. We then add half the width and the height of the radar image to the blipX/Y values so that these values are now positioned relative to the center.

Finally, a new Vector2 object is created and returned, passing back these final calculated X and Y pixel values for the position of our blip icon.

You have been reading a chapter from
Unity 2021 Cookbook - Fourth Edition
Published in: Sep 2021
Publisher: Packt
ISBN-13: 9781839217616
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image