Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
  • Help Room /
avatar image
1
Question by danii956 · Jan 09, 2018 at 01:47 AM · spritegravityphysics2dcircledrawing

Creating a dynamic circle outline around a circle object to indicate radius

Within Gravitational Range Outside Gravitational Range I need to create a circle outline to indicate the "gravity distance" where if you are in a certain range of a circle object (planet), you are in effect of the circle object's gravity.

I have a circle outline sprite that I could put as a child of the circle object that could serve as an indicator. However, I do not know how to scale it properly. If I scale it the same way then, because of the sprite image's different png size, it does not scale properly.

Furthermore, the gravity distance is not strictly determined by the size of the planet. I have a variable called maxGravDist that sets the gravitational range of the planet. I want the circle indicator to change according to this maxGravDist. Here is how I use maxGravDist.

 // Used to give gravity to individual planets
         if (player.GetComponent<Player>().isJumping)
         {
             float dist = Vector3.Distance(transform.position, player.transform.position);
 
             if (dist <= maxGravDist)
             {
                 Vector3 v = transform.position - player.transform.position;
                 player.GetComponent<Rigidbody2D>().AddForce(v.normalized * (1.0f - dist / maxGravDist) * maxGravity);
             }
         }

Is there a way I can resize the outline according to maxGravDist and the size of the planet so that the player will feel the effect of the gravity as soon as it enters the outer circle? Thank you!

P.S. On the other hand, one other solution that I have come up with is to draw the circle using maxGravDist instead of using the circle outline sprite. However, I do not know how to draw a 2D circle using script. If someone can point me to a tutorial that does that, that would be great! Thank you.

planetexample0.png (115.6 kB)
planetexample1.png (119.8 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
2

Answer by danii956 · Jan 10, 2018 at 02:53 AM

Argh.. Why is it so hard to draw a circle in Unity? Well, I found a work around.

 [RequireComponent(typeof(LineRenderer))]
 public class LineRendererEx : MonoBehaviour
 {
     public int vertexCount = 40; // 4 vertices == square
     public float lineWidth = 0.2f;
     public float radius;
 
     private LineRenderer lineRenderer;
 
     private void Awake()
     {
         lineRenderer = GetComponent<LineRenderer>();
         SetupCircle();
     }
 
     private void SetupCircle()
     {
         lineRenderer.widthMultiplier = lineWidth;
 
         float deltaTheta = (2f * Mathf.PI) / vertexCount;
         float theta = 0f;
 
         lineRenderer.positionCount = vertexCount;
         for (int i=0; i<lineRenderer.positionCount; i++)
         {
             Vector3 pos = new Vector3(radius * Mathf.Cos(theta), radius * Mathf.Sin(theta), 0f);
             lineRenderer.SetPosition(i, pos);
             theta += deltaTheta;
         }
     }
 
 #if UNITY_EDITOR
     private void OnDrawGizmos()
     {
         float deltaTheta = (2f * Mathf.PI) / vertexCount;
         float theta = 0f;
 
         Vector3 oldPos = Vector3.zero;
         for (int i=0; i<vertexCount + 1; i++)
         {
             Vector3 pos = new Vector3(radius * Mathf.Cos(theta), radius * Mathf.Sin(theta), 0f);
             Gizmos.DrawLine(oldPos, transform.position + pos);
             oldPos = transform.position + pos;
 
             theta += deltaTheta;
         }
     }
 #endif
 }

OnDrawGizmos() function is not really important. It just allows you to see what the circle will look like while in Unity editor. SetupCircle() draws the actual circle. Public float radius determines the circle's radius while you can add/subtract position.x/position.y to Vector3 pos to move the circle around. lineWidth variable is responsible for the width of the circle and vertexCount variable is number of vertices. The more vertices you have, the more "circle" it will look. If you have 3 vertices, you will have a triangle. Also, make sure to check the loop field in LineRenderer component or else you will have a small gap in the circle.

I got this code/tutorial from Krister Cederlund in YouTube if anybody wants to check it out more in depth.

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

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

Related Questions

Physics 2D disable collision bouncyness 0 Answers

2D planet gravity and Jumping problem 0 Answers

Gravity won't apply after I hit jump, just floats up 0 Answers

How to make instantiated rigidbodies continue moving the in the same direction as destroyed object? 1 Answer

Somthing Weird! Creating filled circle with gradient(alpha) texture 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