Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by wilvis_unity · May 11 at 12:45 PM · movement3djumpmovement scriptwall jump

Can't Jump While Wall Running

I am coding a character movement scripts and I'm trying to code wall running. He is able to run on the wall but I'm trying to get the player to jump between walls.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WallRunning : MonoBehaviour
 {
 
     [Header("Wallrunning")]
     [SerializeField] LayerMask whatIsRunAble;
     [SerializeField] LayerMask whatIsGround;
     [SerializeField] float wallRunForce;
     [SerializeField] float wallRunUpForce;
     [SerializeField] float wallRunSideForce;
     [SerializeField] float wallClimbSpeed;
     [SerializeField] float MaxWallRunTime;
     float wallRunTimer;
 
     [Header("Input")]
     KeyCode jumpKey = KeyCode.N;
     KeyCode upwardsRunKey = KeyCode.CapsLock;
     KeyCode downwardsRunKey = KeyCode.B;
     bool upwardsRun;
     bool downwardsRun;
     float horizontalInput;
     float verticalInput;
 
     [Header("Detection")]
     [SerializeField] float wallCheckDistance;
     [SerializeField] float minJumpHeight;
     RaycastHit leftWallHit;
     RaycastHit rightWallHit;
     bool wallLeft;
     bool wallRight;
 
     [Header("Exiting")]
     [SerializeField] float exitWallTime;
     bool exitingWall;
     float exitWallTimer;
     bool jumping;
 
     [Header("References")]
     [SerializeField] Transform orientation;
     MoveChar mc;
     Rigidbody rb;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         mc = GetComponent<MoveChar>();
     }
 
     // Update is called once per frame
     void Update()
     {
         CheckForWall();
         StateMachine();
     }
 
     private void FixedUpdate()
     {
         
         if (mc.wallRunning)
         {
             WallRunMove();
         }
     }
 
     void CheckForWall()
     {
         wallRight = Physics.Raycast(transform.position, orientation.right, out rightWallHit, wallCheckDistance, whatIsRunAble);
         wallLeft = Physics.Raycast(transform.position, -orientation.right, out leftWallHit, wallCheckDistance, whatIsRunAble);
     }
 
     bool AboveGround()
     {
         return !Physics.Raycast(transform.position, Vector3.down, minJumpHeight, whatIsGround);
     }
 
     void StateMachine()
     {
         horizontalInput = Input.GetAxisRaw("Horizontal");
         verticalInput = Input.GetAxisRaw("Vertical");
      
 THIS IS WHERE THE ERROR IS (I THINK) AND IT HAS TO DO WITH THE exitingWall BOOL
 
         if ((wallLeft || wallRight) && verticalInput > 0 && AboveGround() && !exitingWall)
         {
             /*if (Input.GetKeyDown(jumpKey))
             {
                 WallJump();
             }*/
 
             if (!mc.wallRunning)
             {
                 StartWallRun();
             }            
         }
         else if (exitingWall)
         {
             if (mc.wallRunning)
             {
                 StopWallRun();
             }
 
             if (exitWallTimer > 0)
             {
                 exitWallTimer -= Time.deltaTime;
             }
 
             if (exitWallTimer <= 0)
             {
                 exitingWall = false;
             }
         }
         else
         {
             if (mc.wallRunning)
             {
                 StopWallRun();
             }
             
         }
     }
 
     void StartWallRun()
     {
         mc.wallRunning = true;
     }
 
     void WallRunMove()
     {
         rb.useGravity = false;
         rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
 
         Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;
 
         Vector3 wallForward = Vector3.Cross(wallNormal, transform.up);
 
         if ((orientation.forward - wallForward).magnitude > (orientation.forward - -wallForward).magnitude)
         {
             wallForward = -wallForward;
         }
 
         rb.AddForce(wallForward * wallRunForce, ForceMode.Force);
 
         if (Input.GetKey(upwardsRunKey))
         {
             rb.velocity = new Vector3(rb.velocity.x, wallClimbSpeed, rb.velocity.z);
         }
 
         if (Input.GetKey(downwardsRunKey))
         {
             rb.velocity = new Vector3(rb.velocity.x, -wallClimbSpeed, rb.velocity.z);
         }
 
         if (!(wallLeft && horizontalInput > 0) && !(wallRight && horizontalInput < 0))
         {
             rb.AddForce(-wallNormal * 100, ForceMode.Force);
         }
         
     }
 
     void StopWallRun()
     {
         mc.wallRunning = false; 
         rb.useGravity = true;
     }
 
     void WallJump()
     {
         exitingWall = true;
         exitWallTimer = exitWallTime;
 
         Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;
 
         Vector3 forceToApply = transform.up * wallRunUpForce + wallNormal * wallRunSideForce;
 
         rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
         rb.AddForce(forceToApply, ForceMode.Impulse);
     }
 }
 

Thanks for any help in advance. I've been stuck on this for about 5 hours now (probably should've come here sooner). I wrote in capitols in the code where I think the problem is. It seems to work without the bool but is also leading to other errors as I need to stop the StartWallRun for a small period of time (so far about 0.2 seconds) so I don't get attached back to the wall. Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

279 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

my player can jump in mid air,my charater can jump in mid air how do i fix? 0 Answers

Player keeps spinning after collision 0 Answers

Why does my player weirdly clip into environment when jumping? 0 Answers

Make the Player unable to move to opposite direction 1 Answer

Help with jumping script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges