Find distance from camera perpendicularly to targets transform.right.
Hi! I'm struggling to calculate the distance from a camera to the target transform's right vector. Vector3.Distance won't do the trick here. Looks at the screenshots
This one is the straight distance. That's easy. And this one is with the camera offset to the right or left The distance on Z axis must be the same. The camera can also rotate towards a target and the distance still must remain the same. I'd really appreciate some help on implementing that
Answer by goodguy · May 31, 2018 at 04:30 PM
I came up with the solution almost the moment I asked this question. I've just cast a ray from target position in the direction of transform.right of the target and calculated the shortest distance from camera position to the ray like so
Vector3 direction = target.right;
Vector3 origin = target.position;
Ray ray = new Ray(origin, direction);
float zDistanceToTarget = Vector3.Cross(ray.direction, transform.position - ray.origin).magnitude;
It seems to be working well. But anyway, maybe someone's got a better solution. I'd still be much appreciated
Your answer
Follow this Question
Related Questions
Help with setting up 2D auto scrolling camera using Cinemachine 1 Answer
I want to make the player move in the same direction that the camera moves 0 Answers
Pixel Perfect Camera jittering in Parallax Background 0 Answers
CineMachine Camera Movement is inverse 1 Answer
getting Jittery movement on camera when player rotating and moving in same time 0 Answers