Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

c# - Casting a ray from "reticle" not from the camera itself

:) I am developing a third person adventure game where I am using two cinemachine cameras. The first one is "free-look" camera, that is my main camera for the most of the part. The second camera is "virtual" camera that I am using for aim purposes. As u can see from the video, when I activate my "aim" camera I have a ray that takes from it and point at what direction my item (stick in this example) will be throwed. This is working fine, but it is not 100% accurate, because from what u see in the video, I have simple "reticle" canvas that I am using as "crosshair" at the center of my screen that activates on zooming. Is there any way, to take that ray directly from the reticle and not from the aim camera? I need to be more accurate on throwing.

Here is the video: https://vimeo.com/504000362

I am declaring a cinemachine at the beginning and vector3

[SerializeField] Cinemachine.CinemachineVirtualCameraBase aimCam;
Vector3 forceDirection;

And here is my ray in the update method:

if (ObjectIwantToPickUp)
                
            {
                
                Vector3 aimOrigin = aimCam.State.FinalPosition;
                Vector3 aimDir = aimCam.State.FinalOrientation * Vector3.forward;
                Debug.DrawRay(aimOrigin, aimDir * 100, Color.red);
                RaycastHit hit;
                Vector3 hitPos;
                if (Physics.Raycast(aimOrigin, aimDir, out hit))
                {
                    hitPos = hit.point;
                }
                else
                {
                    hitPos = aimOrigin + aimDir * 99;
                }

                forceDirection = hitPos - ObjectIwantToPickUp.transform.position;
            }

Here is the full code i've made until now: (u can look at throw function and handleInput) - https://hatebin.com/ddqzvavtil

Thanks in advance!

question from:https://stackoverflow.com/questions/65868221/casting-a-ray-from-reticle-not-from-the-camera-itself

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...