Have Raycast rotate on local position with fixed direction
I've asked this problem before with little help a few weeks ago so I decided to rebuild what I wrote from scratch to see if I could resolve the problem.
I want to be able to have a raycast at a fixed position in front of my players at all time for things such as determining which ledges to climb.
Here is a gif to demonstrate the effect I want to achieve: http://imgur.com/a/dh2x9
The top gif is what I want, for it to remain in front of my player at all times. When I begin to move the raycast stretches and moves around my player rather than stay in front.
I think it could be my movement code (which is for now just a copy/paste of Unity's character controller) or my raycast code.
Here is my raycast code:
public Vector3 lHandVectorOrigin = new Vector3(0, 2, 1);
public Vector3 lHandVectorDir = new Vector3(-0.5f, 0f, 0f);
void FixedUpdate () {
RaycastHit lHit;
if (Physics.Raycast (transform.TransformPoint (transform.localPosition + lHandVectorOrigin), transform.TransformPoint (-Vector3.up + lHandVectorDir), out lHit, 1f)){
}
}
void Update () {
Debug.DrawRay (transform.TransformPoint (transform.localPosition + lHandVectorOrigin), transform.TransformPoint (-Vector3.up + lHandVectorDir), Color.red);
}
And for my movement code I just copy/pasted what was here for a quick job: https://docs.unity3d.com/ScriptReference/CharacterController.Move.html Specifically I think this line may have something to do with my raycast not behaving properly:
moveDirection = transform.TransformDirection(moveDirection);
Interestingly when I added a rigidbody or edited my move code the behavior didn't change at all which makes me wonder if the problem is simply in my raycast code.
At the end I'm not even sure if this is the proper way to determine ledges and grabbing so if there are any alternatives I'd gladly listen to those too!
Would Quaternion.eulerAngles be a good place to start? I know very little about Quaternion