How do i make a jump while Wallruning Character controller
i have to states wallruning and walkin and 2 raycasts in the right and left if it hits and not grounded it goes for the wallruning state which have a walldir vector responsible for the movement as the walkin has its own vector the issue is i couldnt make a jump while in the wallrun like i have a method stops the wallrun by switching the state and making the Right and Left variables to false but the issue is the raycast still there so they go back to wallrun if i press space in wall and it goes on and on i tried adding values to the walldir and changing it but it didnt do much ill post the code so u can understand more just ignore the normal movement it works fine thx anyways
public enum States
{
Walking,
WallRunig,
}
States state;
[Header("Controller Settings")]
[SerializeField] CharacterController cc;
[SerializeField] float Movement_Speed;
[SerializeField] float walkSpeed = 15f;
[SerializeField] Camera cam;
[SerializeField] float sprintSpeed = 23f;
[SerializeField] float JumpForce = 15.0f;
[SerializeField] float CrouchSpeed;
bool crouching = false;
[SerializeField] float WallJump = 5f;
float originalHight;
[SerializeField] float crouchHight;
private Vector3 forward, right;
private Vector3 moveDir = Vector3.zero;
private float YonWall;
private Vector3 WallDir;
private bool Right, left;
[SerializeField] float Limit;
[Header("Gravity")]
[SerializeField] float Gravity = 13.0f;
[SerializeField] [Range(.0f, .5f)] float moveSmoothTime = 0.3f;
private Vector2 currentDir, currentDirVelocity;
float velovityY = 0.0f;
[Header("WallRun")]
[SerializeField] private float wallSpeed;
[SerializeField] LayerMask Wall;
[SerializeField] float wallGravity;
[SerializeField] float SideWallforce;
[Header("Camera")]
[SerializeField] private float fov;
void Start()
{
originalHight = cc.height;
state = States.Walking;
}
private void Update()
{
if (state == States.Walking)
{
movementInput();
Movement();
Crouch();
sprint();
}
else if (state == States.WallRunig)
{
wallruning();
}
checkWall();
}
void movementInput()
{
Vector2 Targetdir = new Vector2(Input.GetAxisRaw("Horizontal"),
Input.GetAxisRaw("Vertical"));
Targetdir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, Targetdir, ref currentDirVelocity, moveSmoothTime);
if (cc.isGrounded) velovityY -= Gravity * Time.fixedDeltaTime;
if (!cc.isGrounded)
{
velovityY -= Gravity * Time.fixedDeltaTime;
}
if (Input.GetButtonDown("Jump") && cc.isGrounded) velovityY = JumpForce * Time.fixedDeltaTime;
cc.Move(moveDir * Time.fixedDeltaTime);
}
void Movement()
{
forward = cam.transform.forward.normalized;
right = transform.right.normalized;
moveDir = (forward * currentDir.y + right * currentDir.x) * Movement_Speed;
moveDir.y = velovityY;
}
void Crouch()
{
if (Input.GetKey(KeyCode.C))
{
crouching = true;
}
else crouching = false;
if (crouching)
{
cc.height = crouchHight;
Movement_Speed = CrouchSpeed;
}
else if (!crouching)
{
cc.height = originalHight;
Movement_Speed = walkSpeed;
}
}
void sprint()
{
if (Input.GetKey(KeyCode.LeftShift))
{
Movement_Speed = sprintSpeed;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
Movement_Speed = walkSpeed;
}
}
void checkWall()
{
if (Physics.Raycast(transform.position, transform.right * SideWallforce, out RaycastHit hit, Limit) && !cc.isGrounded)
{
Right = true;
left = false;
WallDir.y = YonWall;
YonWall = wallGravity * Time.fixedDeltaTime;
state = States.WallRunig;
}
else if (Physics.Raycast(transform.position, -transform.right * SideWallforce, out RaycastHit Lhit, Limit) && !cc.isGrounded)
{
left = true;
Right = false;
WallDir.y = wallGravity * Time.fixedDeltaTime;
state = States.WallRunig;
}
else
{
Right = false;
left = false;
state = States.Walking;
}
}
void startwallrun()
{
if (Right == true)
{
WallDir = (forward * wallSpeed + right * SideWallforce);
}
else if (left == true)
{
WallDir = (forward * wallSpeed + -right * SideWallforce);
}
cc.Move(WallDir * Time.deltaTime);
}
void stopWallrun()
{
state = States.Walking;
Right = false;
left = false;
}
void wallruning()
{
startwallrun();
if (Input.GetButtonDown("Jump") )
{
stopWallrun();
velocityY = wallJump;
}
else if (!Right && !left)
{
stopWallrun();
}
}
}
Answer by Mikoa007 · Apr 28, 2021 at 08:19 AM
i cant really understand what you have written, is this about not being able to jump during the wallrun state? if so, you could try making a check using an empty object and then run an if off of that.
Your answer
Follow this Question
Related Questions
FPS Control script problem 0 Answers
Need help with death screen 1 Answer
How to avoid CharacterControllers stepOffset launching my character into space? 1 Answer
Adapt the FirstPersonController Script to Multiplayer 0 Answers
How do I add running and animations to my FPS controller script? 0 Answers