- Home /
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.
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
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.
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.
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
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?
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
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
![](https://koobas.hobune.stream/wayback/20220613013653im_/https://answers.unity.com/themes/thub/images/avi.jpg)