- Home /
How to get Raycast surface normal to align particle system?
Hi there.
Setup is this: I have a particle system, that displays correctly when set to Euler rot (270,0,0).
Now what I want to do is raycast through my camera center and instantiate the particle system where the ray hits, however the particle system needs to be aligned with the surface it's instantiated on.
By aligned, I mean it's "upward" axis should be parallel to the surface's normal.
My code currently only spawns it facing "upward" in world space, mostly because I dont know how to rotate it the way I want it to be:
if(Input.GetButtonDown("Fire1"))
{
RaycastHit hit = new RaycastHit();
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 50.0f))
{
Instantiate (boom, hit.point, Quaternion.Euler (270,0,0));
}
}
(It's supposed to be an explosion into one direction, so it needs to explode/face away from the point of detonation)
What direction in world space is your particle system facing if you use Euler(270,0,0)? Or another way to ask the same question. Or another way to get at the same information, what direction would be normal be facing if Euler(270,0,0) was the correct solution?
Euler(270,0,0) has the particle system facing in World +Y direction.
Answer by 2dkot · Nov 20, 2014 at 04:51 PM
Try to check RaycastHit.normal
I did not try but it could something like this
Instantiate (boom, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
I thought about that, but after googling around it seemed like the hit.normal would basically be the reflection vector of the hitting ray.
ED: Hold on, I did try that. However, then the particle systems were still rotated 270/-90° on the x rot axis. Also, still not sure whether hit.normal actually returns the reflected ray, or the actual surface normal of the hit spot.
@2dkot is close. Given the way you've described it, you should do:
Instantiate (boom, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
Alternately you could create a combined rotation:
var q = Quaternion.FromToRotation(Vector3.up, hit.normal) * Quaternion.Euler(270,0,0);
Instantiate (boom, hit.point, q);
Yes! Vector3.forward did the trick (as woud likewise the second combination).
Thanks a lot guys!
Your answer
Follow this Question
Related Questions
Quarternion.LookRotation is only rotating my game object on one axis 1 Answer
Rotating to Slope 1 Answer
Raycasting and changing player rotation accoording to what is underneath. 0 Answers
Rotate Player to Match Surface Normal While Still Maintaining Y Rotation 0 Answers
Object slope rotation issue 0 Answers