How to get closest point between a collider and a ray/vector that doesn't intersect?
Asked Answered
E

2

0

I’m making a big laser that uses CircleCast to detect collision with objects. And I use LineRenderer to render the laser, a ParticleSystem to render the hit effect. The problem is, if I set the Line’s end point at the hit.point from CircleCast, the laser snaps to a weird angle that is not parallel with the gun’s direction.

How do I get the desired point from this?

Evadnee answered 8/6 at 6:15 Comment(0)
P
0

Hi @Evadnee,

what you are looking for is called vector projection, have a look at this post at wikipedia.

Protect answered 6/6, 2023 at 2:31 Comment(1)

Thank you for the direction, I've figured it out Vector3 toTarget = Vector3.Project((Vector3)hit.point - transform.position, ShootDirection); hitPos = transform.position + toTarget;

Evadnee
M
0

This point is called the “centroid”. It’s the center of the shape that was used to do the cast.

For 2D physics, it’s available simply as hit.centroid.
For 3D physics, you need to manually calculate it using rayOrigin + normalizedRayDirection * hit.distance.

Myca answered 19/4 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.