- Home /
How do you make an wall jump? for 3d games
I am trying to make my character wall jump so if it jumps to an wall it can wall jump but he needs to hit the ground again before wall jump again. Does someone know how to make this or already has an script? Hope someone will help
Answer by thaswasupbreh · Jun 01, 2020 at 02:09 PM
maybe you can use empty objects similar to how the ground check works and put them on the side of the player. then whenever they jump it checks first the ground then the wall and if the wall is the only one that works change a bool to false so it can't work again until the bool is changed back when the player jumps off the ground.
Answer by Artik2442 · Jun 01, 2020 at 02:10 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallRun : MonoBehaviour
{
public GameObject player;
public Camera cam;
public Animator anim;
public Rigidbody rb;
RaycastHit Left;
RaycastHit Right;
Vector3 moveAlong;
public bool NoTurnCam;
public string Havejump;
public float limit;
public float maxDistance;
public float offsetDist;
float direction;
public float force = 500;
public float distanceL;
public float distanceR;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
CharacterMovement Script = player.GetComponent<CharacterMovement>();
if (Input.GetKey(KeyCode.LeftShift) && !Script.touchGround)
{
distanceL = Vector3.Distance(Left.point, transform.position);
distanceR = Vector3.Distance(Right.point, transform.position);
if (Physics.Raycast(player.transform.position, player.transform.right * -1, out Left))
{
if (Left.transform.GetComponent<Collider>() != null)
{
if (Vector3.Dot(Left.normal, Vector3.up) == 0 && distanceL < distanceR && distanceL < maxDistance && Havejump != "From Left" && Left.transform.gameObject.layer == LayerMask.NameToLayer("Ground"))
{
Script.canJump = false;
RunWallL();
}
}
else
{
NoTurnCam = false;
Script.canJump = true;
Havejump = "";
if (direction == -1)
{
anim.SetBool("RunningL",false);
}
else if (direction == 1)
{
anim.SetBool("RunningR",false);
}
direction = 0;
}
}
if (Physics.Raycast(player.transform.position, player.transform.right, out Right))
{
if (Right.transform.GetComponent<Collider>() != null)
{
if (Vector3.Dot(Right.normal, Vector3.up) == 0 && distanceR < distanceL && distanceR < maxDistance && Havejump != "From Right" && Right.transform.gameObject.layer == LayerMask.NameToLayer("Ground"))
{
Script.canJump = false;
RunWallR();
}
}
else
{
NoTurnCam = false;
Script.canJump = true;
Havejump = "";
if (direction == -1)
{
anim.SetBool("RunningL",false);
}
else if (direction == 1)
{
anim.SetBool("RunningR",false);
}
direction = 0;
}
}
if(Input.GetKeyDown(KeyCode.Space) && direction != 0)
{
rb.AddForce(Vector3.up * force);
Script.canJump = true;
if(direction == -1)
{
rb.AddForce(transform.right * force);
Havejump = "From Left";
anim.SetBool("RunningL",false);
direction = 0;
}
else if(direction == 1)
{
rb.AddForce(transform.right * -force);
Havejump = "From Right";
anim.SetBool("RunningR",false);
direction = 0;
}
}
}
else
{
NoTurnCam = false;
Script.canJump = true;
Havejump = "";
if (direction == -1)
{
anim.SetBool("RunningL",false);
}
else if (direction == 1)
{
anim.SetBool("RunningR",false);
}
direction = 0;
}
}
void RunWallL()
{
Debug.Log("Wall running Left!");
moveAlong = Vector3.Cross(Left.normal, Vector3.up);
direction = -1;
rb.velocity = new Vector3 (
rb.velocity.x,
Mathf.Clamp(rb.velocity.y, -limit, limit),
rb.velocity.z
);
transform.position = Left.point + transform.right * offsetDist;
NoTurnCam = true;
anim.SetBool("RunningL",true);
}
void RunWallR()
{
Debug.Log("Wall running Right!");
moveAlong = Vector3.Cross(Right.normal, Vector3.up);
direction = 1;
rb.velocity = new Vector3 (
rb.velocity.x,
Mathf.Clamp(rb.velocity.y, -limit, limit),
rb.velocity.z
);
transform.position = Right.point + transform.right * -offsetDist;
NoTurnCam = true;
anim.SetBool("RunningR",true);
}
}
So, this is a script I've created to make the WallRun. It is imperfect and incomprehensible so use this Website and follow the instructions: WallRun.com It really helps me.
Your answer
Follow this Question
Related Questions
I cant move my character,My character wont move 0 Answers
I'm trying to make a 3D player do a wall jump. Does anyone have advice? 0 Answers
Can someone explain how this piece of code from third person prefab works ? 0 Answers
Physics platform character controller sliding issue NO parenting 0 Answers
Smooth walljumping 0 Answers