Raycast problem/wall climb system
I am trying to make a very simple wall climb system where a raycast detects a wall turn the player to the wall normals and move around, the script works but some times when I stop to move the player and try to move again the wall detection loses the "direction" and starts to raycast to world coordinates.
this first screenshot shows how works properly..
this second shows the error the raycast pointing to the wrong direction. and here is the code..
RaycastHit WallDetector;
public Vector3 PlayerPosition;
public Transform Helper;
Vector3 targetPos;
public float positionOffset = 1;
public float OffsetFromWall;
public float speedMultiplier;
public float rotateSpeed;
void HangWall()
{
Helper.transform.rotation = Quaternion.LookRotation(-WallDetector.normal);
Helper.transform.position = transform.position;
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 h = Helper.right * hor;
Vector3 v = Helper.up * ver;
Vector3 moveDir = (h + v).normalized;
Vector3 origin = transform.position;
float dis = positionOffset;
Vector3 dir = moveDir;
origin += moveDir * dis;
dir = Helper.forward;
float dis2 = 1f;
Debug.DrawRay(transform.position, moveDir, Color.red);
Debug.DrawRay(origin, dir * dis2, Color.blue);
if (Physics.Raycast(origin, dir, out WallDetector, dis2) && WallDetector.transform.tag == "CLIMBWALL")
{
transform.rotation = Quaternion.Slerp(transform.rotation, Helper.rotation, Time.deltaTime * rotateSpeed);
targetPos = WallDetector.point + (WallDetector.normal * OffsetFromWall);
transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * speedMultiplier);
}
}
I just cant find what is wrong since is the first time I am using the raycast, so anyone can help me to understand why is losing the direction?
thanks in advance!
Your answer
Follow this Question
Related Questions
raycast or collision? 1 Answer
Rotated object raycasting in wrong directions!!? 3 Answers
How to stop raycast at first object hit? 2 Answers
Raycast doesn't stay in one place 0 Answers