Wall running using raycasting.

Hey I need help getting my player to snap to a certain place, that is like 1 unit away from the wall he is next to, while he is off the ground.

Here is my code:

using UnityEngine;
using System.Collections;
 
public class TP_Raycast : MonoBehaviour
{
	public static TP_Raycast Instance; 
	
    	//Creates a ray
    	private Ray ray;
    	
    	//Creates a RaycastHit
    	private RaycastHit hit = new RaycastHit();
     
    	//Get this GameObject's transform
    	private Transform playerTrans;
     
    	void Awake()
    	{
    		//get this Transform
    		playerTrans = this.GetComponent<Transform>();
    	}
     
    	// Update is called once per frame
    	void Update ()
    	{
    		//Ray to the left of player
    		//recreate the ray every frame AND draw debug line for the ray
    		ray = new Ray(playerTrans.position, transform.TransformDirection(Vector3.left));
    		Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.left), Color.green);
    		 
    		//Casts a ray to see if something has hit it
    		if(Physics.Raycast(ray.origin, ray.direction, out hit, 1))
    		{
    			//Collision has happened, print the distance and name of the object into the console
    			Debug.Log(hit.collider.name);
    			Debug.Log(hit.distance);
    			SnapToWalls();
    		}
    		else
    		{
    			//the ray isn't colliding with anything
    			Debug.Log("none");
    		}
    	}
    	
    	void SnapToWalls()
    	{
    		//variable for the distance of the ray (never greater than 1)
    		var rayDist = hit.distance;
    		Debug.Log(rayDist);
    		//If the players raycasting hit a wall (always less than one if so)
    		if(rayDist > 0)
    		{
    			if(rayDist < 1)
    			{
    				//Only if player left the ground (jumped)
    				if(!TP_Controller.CharacterController.isGrounded)
    				{
    					//change gravity
    					TP_Motor.Gravity = 11f;
    					//then have the player forced forward somewhat (even if they dont push forward - more so if they do)
    					TP_Controller.CharacterController.Move(transform.TransformDirection(Vector3.forward) * Time.deltaTime * 2);
    					//and output blah into the console
    					Debug.Log("BLAH");
    //HERE IS WHERE I NEED CODING TO MAKE MY PLAYER WALL WALK :(
    				}
    			}
    		}
    		else
    		{
    			//go back to normal
    			TP_Motor.Gravity = 21f;
    		}
    	}

}

what this does right now is move the player forwards a little, and if they are pressing forward it moves them faster, later ill animate the character like he is running along the wall.

but i need to make it like he is only moveable in two axis’s of the world, or maybe only one, if he is going straight up a wall with a ray thats going forward detecting a wall.

for now I cant think of a way to do it, I tried clamping but couldnt quite figure it out, how to get the raw like world coordinates of my player then to force any (X or Z) to be stuck at a number I set. that way he moves sort of along the surface of a wall.

anyone know how the make my player move along a wall, still being effected by gravity from my motor? what are other ways to make that effect?

I’m not sure how you use the Raycast function since it’s not in the code you’ve posted. Just to make sure you understand how the raycast function works: Raycast returns a boolean which is true when the ray hits something or false what it doesn’t. If the Raycast function returns false the data in your RaycastHit structure contains garbage since there was no hit. If you raycast only 1 unit it will almost never report a hit at 1 unit distance.

I guess your “else” block should be the else of your if (Raycast(...)) block

i think you should create a new function in TP_Motor called “MoveOnWall()”
in this method you force the player into the wall(to create some friction),and then you CharacterController.Move to make him go upward. Also make the gravity 0 if necessary.