Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 UBrownie · Oct 13, 2020 at 08:00 AM · unity 5linerendererdrawing

Adding a dot within bounds of sprite

I just started using unity and I want to add a single dot to a game object. I tried using line renderer with a code from online:

void Update() { if (Input.GetMouseButtonDown(0)){ CreateDot(); } }

     void CreateDot()
     {
         currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);
         lineRenderer = currentLine.GetComponent<LineRenderer>();
         fingerPositions.Clear();
         fingerPositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
         fingerPositions.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition));
         lineRenderer.SetPosition(0, fingerPositions[0]);
         lineRenderer.SetPosition(1, fingerPositions[1]);
     }


This creates a dot but single fixed position.

What I need is to create a dot within bounds of another game object where mouse is clicked. Thanks for any help.

Comment
Add comment · Show 3
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 Klarzahs · Oct 13, 2020 at 09:22 AM 0
Share

If you only want to create such a dot if you click on a gameobject, why not use a raycast? You basically shoot a ray from your mouse position, if it hits the GO, create a dot at this position

avatar image UBrownie Klarzahs · Oct 13, 2020 at 10:32 AM 0
Share

Sorry, I'm completely new to unity, that's why I don't know how to use it. But by playing with the previous code and keeping only:

 currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);

and changing the position vector I was able to create a dot where the mouse is. but it creates it all over the screen. I want to keep to certain places only.

avatar image Klarzahs UBrownie · Oct 13, 2020 at 10:49 AM 0
Share

See my answer/code example below

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Klarzahs · Oct 13, 2020 at 10:49 AM

Code example for a raycast to hit your gameobject, and to then create the dot at this position:

 void Update(){
   if(Input.GetMouseButtonDown(0)){
     Ray ray = Camera.main.ScreenPointToRay(Input.MousePosition);
     if(Physics.Raycast(ray, out RaycastHit hit) && hit.gameObject == YourGameObject){
       currentLine = Instantiate(linePrefab, hit.point, Quaternion.identity);
     }
   }
 }

You check, whether the mouse is pressed, then shoot a raycast from the current mouse position and direction of the camera into the scene. If it hits something and this something is also your gameobject, you create a point at the resulting coordinates. Otherwise, it does nothing :)
For more information on raycasts, have a look at this tutorial.
Standard browser disclaimer applies.

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 Klarzahs · Oct 13, 2020 at 10:50 AM 1
Share

Please note that you'll have to add a collider of some sorts to your gameobject, otherwise the raycast wont hit anything. Also, if another object (with a collider) is in front, it will hit and return the foremost object, aka not yours

avatar image UBrownie · Oct 13, 2020 at 12:43 PM 0
Share

Great, I just had to change hit.gameObject == YourGameObject to hit.collider == GetComponent"Collider"()

and it created the dot right where I clicked. The problem is that the dot is invisible. The reason I found is that the dot takes the z axis of the camera which is less than the object I'm trying to draw on. and if I make the z axis higher than the object the object becomes hidden because of the camera. How can I resolve this?

avatar image Klarzahs UBrownie · Oct 13, 2020 at 01:43 PM 1
Share

If i understood you correctly, you can move the dot towards the camera. So you take the vector from the object to the camera, normalize it and scale it to e.g. 0.1 length. Then you add it to the dot. You can also just use the z component of this offset vector

avatar image UBrownie Klarzahs · Oct 14, 2020 at 05:26 AM 0
Share

Sorry, The previous code solution is okay. $$anonymous$$y problem was that I had a 2D collider on my object. The dot was drawn because of another code I had written. I changed the 2D collider to 3D collider and it worked like charm. Thank you again.

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

238 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 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

Starting and Ending points of LineRenderer 1 Answer

Intersection between Line Render and image 0 Answers

Neon / Glow effect on lines in 2d 1 Answer

White outline around tree leaves after upgrade to Unity 5 1 Answer

Drawing and saving during gameplay 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