1
votes

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

1
maybe you forgot to transform something? - aybe
@aybe What do you mean by that? - James Barnett
Based on the docs, point is where the ray hit the collider, whereas transform is the location of the hit collider's transform. - ps2goat
@ps2goat Yes, I figured it out at the same time as you! XD - James Barnett

1 Answers

1
votes

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