• 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 ZentZo · Nov 11, 2013 at 10:00 PM · colliderdrawing

Drawing line by spawning blocks in runtime

My team is trying to make a puzzle game where player draw line on screen as a wall/obstruct to change some ball's moving direction.

here's my solution so far:

 void Update () 
     {
         //if(canDraw == true)
         //{
             if(Input.GetKeyDown(KeyCode.Mouse0))
             {
                 StartCoroutine("DrawingBlocks");
             }
             if(Input.GetKeyUp (KeyCode.Mouse0))
             {
                 StopCoroutine("DrawingBlocks");
             }
         //}
     
     }
     void DrawSwitchOff()
     {
         canDraw = false;
     }
     void DrawSwitchOn()
     {
         canDraw = true;
     }
     IEnumerator DrawingBlocks()
     {
         mousePos1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         while(true)
         {
             
             yield return new WaitForSeconds(0.01f);//block spawning interval
             mousePos2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             blockDrawPos = new Vector3 ((mousePos1.x + mousePos2.x) / 2, (mousePos1.y + mousePos2.y) / 2, 0);
             blockRotation = new Vector3(mousePos2.x - mousePos1.x, mousePos2.y - mousePos1.y, 0);
             // don't draw if mouse didn't move
             if(mousePos1 != mousePos2)
             {
                 GameObject clone;
                 if(canDraw == true)
                 {
                     //spawn a block in middle of 2 mouse position
                     clone = Instantiate(Block, blockDrawPos, Quaternion.LookRotation(blockRotation)) as GameObject;
                     //send message to ink monitor
                     inkUsed.SendMessage("inkUsed",1);
                     //change spawned block's scale to fill the distance between 2 mouse position
                 clone.gameObject.transform.localScale += new Vector3 (0, 0, Vector3.Distance(mousePos1, mousePos2));
                 }
             }
             mousePos1 = mousePos2;
         }
         
     }


It detects mouse position (before and after) every short amount of time and spawn a block(with a box collider) in between and change its scale to fill the gap, It works fine but the line seems too jagged sometime if the mouse is moving at a very low speed. Also because of the jagged line the collider is also jagged so the balls is bouncing weird. My question is: Is there a better way to achieve this to make the line looks smooth? And make the collider boxes connect smoothly so the balls wouldn't bounce to unwanted direction. Thank you.

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 Bryan-Legend · Nov 11, 2013 at 10:50 PM

Why not rotate the boxes to match the direction of the line? Or just make one box and stretch it in the direction of the line?

Comment
Add comment · Show 2 · 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 ZentZo · Nov 12, 2013 at 12:52 AM 0
Share

I did rotate it, currently it's a series of rotated and stretched boxes to make the line the player drew, but the result doesn't look good enough because of the jagged line.

actually I can live with the jagged line, it's the collider boxes that is jagged and not perfor$$anonymous$$g like I want that bothers me.

avatar image ZentZo · Nov 12, 2013 at 01:14 AM 0
Share

I did rotate it, currently it's a series of rotated and stretched boxes to make the line the player drew, but the result doesn't look good enough because of the jagged line.

actually I can live with the jagged line, it's the collider boxes that is jagged and not perfor$$anonymous$$g like I want that bothers me.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Internal collisions 1 Answer

how to draw on the screen with colliders 0 Answers

How to make 3D colliders on sprites? 0 Answers

Test if point is in Collider/Trigger 10 Answers

Triggering platform animation on colliding with Button 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