EDIT: Here is a gif that helps visualize what I am trying to achieve.
As you can see my hands snap to the ledge whenever the raycast detects an object. However when I rotate my character the raycasts don’t stay in front of my character meaning I can only snap from 1 specific rotation.
So I’ve been searching online everywhere but it seems my specific problem I can’t solve on my own.
I am making a raycast to determine when a 3d character controller has detected a ledge and can grab it. So I made a raycast that has a specific position (in front of the player and facing down). Yet now I am trying to get the raycast to always stay in front of my character rather than statically stay in the same position (rotate along with my character).
I don’t know how to place a raycast in a specific position without using Vector 3. I feel like the problem could be in the direction.
Here’s what I have so far (you can easily copy/paste this and it should work instantly):
//bools that will be triggered by hands raycast
public bool leftHandIK;
public bool rightHandIK;
//to move hands to ledge
public Vector3 leftHandPosition;
public Vector3 rightHandPosition;
//to ensure hands don't go inside wall/ledges
public Vector3 leftHandOffset;
public Vector3 rightHandOffset;
//to make hands properly rotate alongside ledge
public Quaternion leftHandRotation;
public Quaternion rightHandRotation;
void FixedUpdate () {
RaycastHit lHit;
RaycastHit rHit;
//LeftHand IK check
//set a raycast at our location, add so the raycast starts just above our head + a little forward & casts it downwards & move that raycast to the left side
//if the leftside raycast hits something...
if(Physics.Raycast (transform.position + new Vector3(0.0f, 2.0f, 1f), -transform.up + new Vector3 (-0.5f, 0.0f, 0.0f), out lHit, 1f)){
leftHandIK = true;
leftHandPosition = lHit.point - leftHandOffset; //the position of the lhand is wherever the raycast hits - offset so we dont go inside walls/ledges
leftHandRotation = Quaternion.FromToRotation (Vector3.forward, lHit.normal); //this somehow calculates an appopropriate rotation for our hand
}else{
leftHandIK = false; //else lHandIK is not detecting anything
}
//RightHand IK check
if(Physics.Raycast (transform.position + new Vector3 (0.0f, 2.0f, 1f), -transform.up + new Vector3 (0.5f, 0f, 0f), out rHit, 1f)){
rightHandIK = true;
rightHandPosition = rHit.point - rightHandOffset;
rightHandRotation = Quaternion.FromToRotation (Vector3.forward, rHit.normal);
}else{
rightHandIK = false;
}
}
void Update () {
//Lefthand IK ray drawn
Debug.DrawRay (transform.position + new Vector3 (0.0f, 2.0f, 1f), -transform.up + new Vector3 (-0.5f, 0.0f, 0.0f), Color.green);
//Righthand IK ray drawn
Debug.DrawRay (transform.position + new Vector3 (0.0f, 2.0f, 1f), -transform.up + new Vector3 (0.5f, 0.0f, 0.0f), Color.green);
}