Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - Why is my raycast hit point expected output different to the actual output?

I have been trying to make a character controller with inverse kinematics, and I am using ray casts to determine the target points for the bones to attach to, but I am having some issues with the ray casting hit information being changed.

What exactly is happenning? Well I am shooting out a raycast in the direction that the raycast point is facing. Once it hits the ground, it sets the anchor position to the hit point position. Except for the fact that my anchor point position is completely different to my hit point position even after the raycasting.

public Transform[] anchors;
void RayOut()
{
    RaycastHit hit; // Create Ray Instance
    if (Physics.Raycast(transform.position, transform.forward, out hit)) // Launch The Instance
    {
        Debug.DrawLine (transform.position, hit.point);
        switch (gameObject.name) // Check This Name & Assign Positions
        {
            case "TR":
                anchors[0].position = hit.transform.position;
                break;
            
            case "TL":
                anchors[1].position = hit.transform.position;
                break;
            
            case "BL":
                anchors[2].position = hit.transform.position;
                break;
            
            case "BR":
                anchors[3].position = hit.transform.position;
                break;
        }
    }
}

private void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
        RayOut();
}

The point where the white line intersects the floor is the expected point that the anchor should be, but the three arrows are where it actually is.

Screenshot

question from:https://stackoverflow.com/questions/65909523/why-is-my-raycast-hit-point-expected-output-different-to-the-actual-output

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

1 Reply

0 votes
by (71.8m points)

I figured out the reason, I was using transform.position, when in reality it is supposed to be point. (Credit to ps2goat for also finding it).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...