- Home /
Instantiate based on RaycastHit.normal
Im working on a weapon script, in javascript. And im trying to implement some particles to be instantiated where the bullet/raycast hits. I have got the position of the particles working fine. But im having trouble with the rotation.
The problem is making the instantiated particles to be based on the normal direction of the surface it hits.
So i tryed to use RaycastHit.normal, but its Vector3. And the Object.Instantiate rotation is Quaternion.
Do i need to convert the Vector3 value to Quaternion, if so how? Or is there another way to achive what im trying to do?
Answer by duck · May 08, 2010 at 10:52 PM
Yes, you can create a rotation which would make the object point in the direction of the hit normal using the Quaternion.LookRotation function. It does exactly what you need.
Eg:
var startPos = hit.point;
var startRot = Quaternion.LookRotation(hit.normal);
Object.Instantiate( particlePrefab, startPos, startRot );
The particle system's forward (z+) direction will be pointing along the hit normal vector.
How do I do this in 2D such that the instance is instantiated with it's y
/up
direction along the normal, ins$$anonymous$$d of z
/forward
?
Your answer
Follow this Question
Related Questions
Quaternion.LookRotation 2 Answers
"Look rotation viewing vector is zero" error 2 Answers
Quaternion.LookRotation precision error 1 Answer
Turret rotation on one axis problems 2 Answers
Simple Transform question, using Quaternion.LookRotation 0 Answers