- Home /
Question by
Bavley2004 · Jul 12, 2021 at 12:18 PM ·
fpswall jump
Wall Running help
I want to make wall running mechanics in my game, I was somewhat successful but I have some inconsistencies with the jumping, one time it jumps to the side really fast and other times it jumps really slow, also sometimes it detects that it is wall running after I jump off a wall. Would really appreciate the help :)
private Rigidbody rb;
private PlayerController controller;
public Transform playerBody;
public float minJumpHeight;
public float wallRunGravity;
public float wallRunJumpForce;
public float wallRunSpeed;
private Vector3 wallRunJumpDirection;
public bool isWallRunning;
private bool wallIsThere;
private bool isJumping;
private void Awake()
{
rb = GetComponent<Rigidbody>();
controller = GetComponent<PlayerController>();
}
private void Update()
{
isJumping = Input.GetButton("Jump");
}
void FixedUpdate()
{
if (CanWallRun())
{
if (wallIsThere)
{
isWallRunning = true;
StartWallRun();
}
else
{
isWallRunning = false;
StopWallRun();
}
}
else
{
isWallRunning = false;
StopWallRun();
}
}
bool CanWallRun()
{
return !Physics.Raycast(playerBody.position, Vector3.down, minJumpHeight);
}
void StartWallRun()
{
rb.useGravity = false;
rb.AddForce(Vector3.down * wallRunGravity);
if (rb.velocity.magnitude < controller.maxSpeed && Input.GetAxisRaw("Vertical") > 0)
rb.AddForce(playerBody.forward * wallRunSpeed * Time.deltaTime);
if (isJumping)
{
wallIsThere = false;
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(Vector3.up * 150f * Time.deltaTime, ForceMode.Impulse);
rb.AddForce(wallRunJumpDirection * wallRunJumpForce * Time.deltaTime, ForceMode.Impulse);
}
}
void StopWallRun()
{
rb.useGravity = true;
}
private void OnCollisionStay(Collision collision)
{
if (collision == null)
{
return;
}
for (int i = 0; i < collision.contactCount; i++)
{
if (isWall(collision.GetContact(i).normal))
{
wallIsThere = true;
}
else
{
wallIsThere = false;
}
}
}
bool isWall(Vector3 Wallnormal)
{
float wallAngle = Vector3.Angle(Vector3.up, Wallnormal);
wallRunJumpDirection = transform.up + Wallnormal;
return wallAngle == 90f || wallAngle == -90f;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613040205im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidy body 3d wall jump 0 Answers
FPS Advance Gun Script 1 Answer
machinegun don t work 2 Answers
FPS Webplayer problem 0 Answers
GetComponent not Returning 1 Answer