James Barnett
James Barnett

Reputation: 571

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

Upvotes: 1

Views: 882

Answers (1)

James Barnett
James Barnett

Reputation: 571

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).

Upvotes: 1

Related Questions