• 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 /
avatar image
0
Question by wilvis_unity · May 18 at 09:30 AM · raycast3dmovement scriptwallsforcemode

Running on Walls making my movement weird

Hi, I have coded the unity movement for my character. I made it so they can wall run bit it seems that wall running is somehow making my character walk weird. After wall running (if I wall run with the wall on my right) it will apply a force 90 degrees when I'm walking on the ground. The unknown force changes when each button is pressed (is W then apply both the W force and a force pushing it to the right). And the same happens with running on the left wall but it is at a -90 degree angle. Please help I've been stuck on this for a whole week. Thanks

This is the movement code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveChar : MonoBehaviour
 {
 
     [Header("Movement")]
     float moveSpeed;
     [SerializeField] float walkSpeed;
     [SerializeField] float sprintSpeed;
     [SerializeField] float slideSpeed;
     [SerializeField] float wallRunSpeed;
 
     float desiredMoveSpeed;
     float lastDesiredMoveSpeed;
 
     [SerializeField] float groundDrag;
 
     [Header("Jumping")]
     [SerializeField] float jumpForce;
     [SerializeField] float jumpCoolDown;
     [SerializeField] float airMultiplier;
     bool readyToJump;
     [HideInInspector] public bool doubleJump;
 
     [Header("Crouching")]
     [SerializeField] float crouchSpeed;
     [SerializeField] float crouchYScale;
     float crouchYStart;
     bool exitingJump;
 
     [Header("Slope Check")]
     [SerializeField] float maxSlope;
     RaycastHit slopeHit;
 
 
     [Header("Ground Check")]
     [SerializeField] float playerHeight;
     [SerializeField] LayerMask whatIsGround;
     bool grounded;
 
     [Header("Keybinds")]
     KeyCode jumpKey = KeyCode.Space;
     KeyCode walkKey = KeyCode.LeftShift;
     KeyCode crouchKey = KeyCode.C;
 
 
     [SerializeField] Transform orientation;
     [SerializeField] Transform camRotate;
 
     float horizontalInput;
     float verticalInput;
 
     Vector3 moveDirection;
 
     Rigidbody rb;
     MovementState state;
     enum MovementState
     {
         walking,
         sprinting,
         crouching,
         sliding,
         wallRunning,
         air
     }
 
     [HideInInspector] public bool sliding;
     [HideInInspector] public bool wallRunning;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         ResetJump();
         crouchYStart = transform.localScale.y;
     }
 
     // Update is called once per frame
     void Update()
     {
         grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
 
         MyInput();
         SpeedControl();
         StateHandler();
 
         if (grounded)
         {
             rb.drag = groundDrag;
         }
         else
         {
             rb.drag = 0;
         }
     }
 
     private void FixedUpdate()
     {
         MovePlayer();
     }
 
     void MyInput()
     {
         horizontalInput = Input.GetAxisRaw("Horizontal");
         verticalInput = Input.GetAxisRaw("Vertical");
 
         if (Input.GetKeyDown(jumpKey) && readyToJump && (grounded || doubleJump))
         {
             readyToJump = false;
             Jump();
             Invoke(nameof(ResetJump), jumpCoolDown);
         }
 
         if (grounded && !Input.GetKey(jumpKey))
         {
             doubleJump = false;
         }
 
         if (Input.GetKeyDown(crouchKey))
         {
             transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
             rb.AddForce(Vector3.down * 5f, ForceMode.Impulse);
         }
 
         if (Input.GetKeyUp(crouchKey))
         {
             transform.localScale = new Vector3(transform.localScale.x, crouchYStart, transform.localScale.z);
         }
     }
 
     void StateHandler()
     {
         if (wallRunning)
         {
             state = MovementState.wallRunning;
             desiredMoveSpeed = wallRunSpeed;
             doubleJump = false;
         }
 
         if (sliding)
         {
             state = MovementState.sliding;
 
             if (OnSlope() && rb.velocity.y < 0.1f)
             {
                 desiredMoveSpeed = slideSpeed;
             }
             else
             {
                 desiredMoveSpeed = sprintSpeed;
             }
         }
         else if (Input.GetKey(crouchKey))
         {
             state = MovementState.crouching;
             desiredMoveSpeed = crouchSpeed;
         }
         else if (grounded && Input.GetKey(walkKey))
         {
             state = MovementState.walking;
             desiredMoveSpeed = walkSpeed;
         }
         else if (grounded)
         {
             state = MovementState.sprinting;
             desiredMoveSpeed = sprintSpeed;
         }
         else
         {
             state = MovementState.air;
         }
 
 
 
         if (Mathf.Abs(desiredMoveSpeed - lastDesiredMoveSpeed) > 4f && moveSpeed != 0)
         {
             StopAllCoroutines();
             StartCoroutine(SmoothlyLerpMoveSpeed());
         }
         else
         {
             moveSpeed = desiredMoveSpeed;
         }
         lastDesiredMoveSpeed = desiredMoveSpeed;
     }
 
     IEnumerator SmoothlyLerpMoveSpeed()
     {
         float time = 0;
         float difference = Mathf.Abs(desiredMoveSpeed - moveSpeed);
         float startValue = moveSpeed;
 
         while (time < difference)
         {
             moveSpeed = Mathf.Lerp(startValue, desiredMoveSpeed, time / difference);
             time += Time.deltaTime;
             yield return null;
         }
 
         moveSpeed = desiredMoveSpeed;
     }
 
     void MovePlayer()
     {
         moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
 
         if (OnSlope() && !exitingJump)
         {
             rb.AddForce(GetSlopeMoveDirection(moveDirection) * moveSpeed * 20f, ForceMode.Force);
 
             if (rb.velocity.y > 0)
             {
                 rb.AddForce(Vector3.down * 80f, ForceMode.Force);
             }
         }
         else if (grounded)
         {
             rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
         }
         else if (!grounded)
         {
             rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
         }
 
         if (!wallRunning)
         {
             rb.useGravity = !OnSlope();
         }
 
     }
 
     void SpeedControl()
     {
 
         if (OnSlope() && !exitingJump)
         {
             if (rb.velocity.magnitude > moveSpeed)
             {
                 rb.velocity = rb.velocity.normalized * moveSpeed;
             }
         }
         else
         {
             Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
 
             if (flatVel.magnitude > moveSpeed)
             {
                 Vector3 limitVel = flatVel.normalized * moveSpeed;
                 rb.velocity = new Vector3(limitVel.x, rb.velocity.y, limitVel.z);
             }
         }
     }
 
     void Jump()
     {
         exitingJump = true;
 
         rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
 
         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
 
         doubleJump = !doubleJump;
     }
 
     void ResetJump()
     {
         readyToJump = true;
         exitingJump = false;
     }
 
     public bool OnSlope()
     {
         if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight * 0.5f + 0.3f))
         {
             float angle = Vector3.Angle(Vector3.up, slopeHit.normal);
             return angle < maxSlope && angle != 0;
         }
         return false;
     }
 
     public Vector3 GetSlopeMoveDirection(Vector3 direction)
     {
         return Vector3.ProjectOnPlane(direction, slopeHit.normal).normalized;
     }
 }
 
 This is the wall running code

 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;
 
     [Header("Input")]
     KeyCode jumpKey = KeyCode.Space;
     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;
 
     [Header("Gravity")]
     [SerializeField] bool useGravity;
     [SerializeField] float gravityCounterForce;
 
     [Header("References")]
     [SerializeField] Transform orientation;
     MoveChar mc;
     Rigidbody rb;
     [SerializeField] Transform cam;
 
     // 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");
         if ((wallLeft || wallRight) && verticalInput > 0 && AboveGround() && !exitingWall)
         {
             if (Input.GetKeyDown(jumpKey))
             {
                 WallJump();
             }
         }
 
         if ((wallLeft || wallRight) && verticalInput > 0 && AboveGround() && !exitingWall)
         {
             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;
         rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
         if (wallRight)
         {
             cam.localRotation = Quaternion.Euler(0, 0, 10f);
         }
         if (wallLeft)
         {
             cam.localRotation = Quaternion.Euler(0, 0, -10f);
         }
 
     }
 
     void WallRunMove()
     {
         rb.useGravity = useGravity;
 
         Vector3 wallNormal = wallRight ? rightWallHit.normal : leftWallHit.normal;
 
         Vector3 wallForward = Vector3.Cross(wallNormal, transform.up.normalized);
 
         if ((orientation.forward - wallForward).magnitude > (orientation.forward - -wallForward).magnitude)
         {
             wallForward = -wallForward;
         }
 
         rb.AddForce(wallForward * wallRunForce, ForceMode.Force);
 
         /*if (!(wallLeft && horizontalInput > 0) && !(wallRight && horizontalInput < 0))
         {
             rb.AddForce(-wallNormal * 100, ForceMode.Force);
         }
 
         if (useGravity)
         {
             rb.AddForce(transform.up * gravityCounterForce, ForceMode.Force);
         }*/
     }
 
     void StopWallRun()
     {
         mc.wallRunning = false; 
         cam.localRotation = Quaternion.Euler(0, 0, 0);
     }
 
     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);
         mc.doubleJump = !mc.doubleJump;
     }
 }
 

Thank you soooooooo much for any help

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

232 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

Related Questions

How to restrict movement towards a collider. 1 Answer

3rd person carrying objects,3rd person picking up objects 0 Answers

Smooth Movement with set distance 1 Answer

How to make a top down character snap to different directions 0 Answers

Drawing a 3d Cone ray 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