- Home /
Offseting insantiated object
I am working on a gun script in my game and I am using raycasting currently to shoot. So when I am instantiating the impact prefab, it is putting it right where the raycast hit and I am trying to figure out how to offset the instantiated object so instead of being directly on the object, it hovers a little bit away from the object.
This is a snippet of my code
void Shoot()
{
ammo--;
RaycastHit hit;
Transform cam = GameObject.FindGameObjectWithTag("MainCamera").transform;
if (Physics.Raycast(cam.position, cam.forward, out hit, 1000))
{
Instantiate(impactPrefab, hit.point, Quaternion.FromToRotation(-transform.forward, hit.normal) * transform.rotation, null);
}
}
if you can help out, thank you.
Answer by BastianUrbach · Feb 22, 2018 at 09:37 AM
Replace hit.point with hit.point - hit.normal * distance where distance is the desired distance from the point.
Your answer
Follow this Question
Related Questions
How can I continue to instantiate an object after deleting 0 Answers
How to Instantiate a prefab on the surface of a curved structure or mesh 1 Answer
Use raycast to see if something else than terrain is under 1 Answer
Distribute terrain in zones 3 Answers
Find a random location, spawn an object there, do it again. 0 Answers