How to project raycast from HitPoint to the top edge of an object and place a temporary anchor on that coordinate?

Hi. First of all, I would like to thank this community for being so helpful and supportive with new game developers such as myself.

Well, I am building a 3rd person parkour game and I have come across a bit of an issue. At the moment, I’ve got everything working well, locomotion, climbing, vaulting, all that. I am using “anchors” for target matching, but the only problem here is that I want to make the system a bit more dynamic and add some sort of algorithm to calculate the top edge of the object I want to climb on, rather than adding thousands of anchors for target matching.

Ok, so, in this case I’ve got my player raycasting forward until it hits an object with a certain tag. I would like that once the raycast hits the object, that hitpoint to shift its position upwards, until reaching the top edge of my climb object. Then, I would like to somehow return the coordinates of that hitpoint and create a temporary “anchor” on those exact coordinates to be used with my “animator.MatchTarget(…)” function.

This is what I’ve got so far. I can’t seem to find a proper way to write it, as I am relatively new to Unity, implicitly new to C#:


if (Physics.Raycast(transform.position, fwd, out hitInfo, 4.5f))					
{	
    if(hitInfo.transform.tag == "Climb")
    {	
	while(hitInfo.transform.tag =="Climb")
	{       
                //shift the hitpoint upwards until it reaches the end of the object 
		hitInfoEdge = hitInfo.transform.position + new Vector3(0, 1, 0);
		Debug.DrawRay (transform.position, hitInfoEdge.point, Color.blue);
	}
	print("There is something in front of the object!");

        //**add code to place a temporary anchor in the same place as hitInfoEdge**

	animator.MatchTarget(anchor.position, anchor.rotation, AvatarTarget.RightHand, new MatchTargetWeightMask(new Vector3(1, 1, 1), 0), animator.GetFloat("MatchStart"), animator.GetFloat("MatchEnd"));
						
	animator.SetBool("ClimbObstacle_1", true);
}

You shouldn’t need that while loop (and I’m not sure it is actually doing what you expect it to be doing).

The hit info object will have a collider attached, so you can use its bounds.size.y to get the height of the object. With that and the position of the object, you should be able to find a point along the top edge.