- Home /
Instantiate a little before the end of the raycast or on screen
I'm raycasting and instantiating an object at the end of the raycast I want this object to spawn slightly earlier in the raycast instead of at the far end of the raycast, how can I achieve that without increasing size of colliders etc?
Here is what I have so far:
if (Input.GetMouseButtonDown(0))
{
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, NormalMask))
{
print("Mousepos" + Input.mousePosition);
target = hit.point;
print(hit.point);
Vector3 pz = target;
pz.y += 0.1f;
Instantiate(MouseClickIdentifier, pz, Quaternion.Euler(-90, 0, 0));
}
}
It would be more ideal if I can just instantiate a sprite on the 2d screen when I press with my mouse on the spot, basically I want a sprite to appear where I click on the screen, but doing it with raycasts puts it inside objects as in the sprite object it says to lookat the camera to make it look flat, the camera can rotate so it has to be dynamically done.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191755im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make an instantiated object match the slope of the terrain 1 Answer
Raycast Specific Object/Instantiate Explosion 1 Answer
Instantiate prefab from original position to click mouse point position 0 Answers
Instantiate gameobject a certain distance away from transform 2 Answers
How do I instantiate a projectile along the path of a raycast? 0 Answers