Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Physics issues in 2.5D platformer

Discussion in 'Physics' started by jasperkeithfoster, Apr 11, 2018.

  1. jasperkeithfoster


    Apr 11, 2018
    I marked this as a physics issue, but I'm not positive that's where the problem lies. Please forgive me if this is posted in the wrong place.

    I'm running into a strange bug when play testing my player movement script. Currently, the player can run right/left, jump, and when the player jumps into a wall, they enter a "wall slide" where their vertical velocity is reduced to -1 so they very slowly slide down the wall until they either A) hit the jump button, resulting in a vault away from the wall, or B) hit the "s" key, which exits the wall slide. However, if there is a wall floating in the air, and the player slides all the way to the bottom of the wall, right as the top edge of the player's box collider meets the bottom edge of the floating wall, the player gets flung beneath the wall.

    Anyone know why this could be happening?

    In the attached image, the blue represents what I want it to do, upon reaching the bottom edge of the wall. The red is what it currently does, for some reason or another.

    I'll post my code here for reference.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    4. public class PlayerMotor : MonoBehaviour
    5. {
    6.     //Variables
    7.     private Vector3 moveVector;
    8.     private Vector3 lastMove;
    9.     public float speed = 8;
    10.     public float jumpForce = 12;
    11.     public float gravity = 25;
    12.     private float verticalVelocity;
    14.     private void Start()
    15.     {
    16.         //Get Character Controller
    17.         controller = GetComponent<CharacterController> ();
    18.     }
    20.     private void Update()
    21.     {
    23.         //Set movement vectors
    24.         moveVector =;
    25.         moveVector.x = Input.GetAxis ("Horizontal") * speed;
    27.         //Apply jump if controller is on the ground
    28.         if (controller.isGrounded)
    29.         {
    30.             verticalVelocity = -1;
    32.             if (Input.GetKeyDown (KeyCode.Space))
    33.             {
    34.                 verticalVelocity = jumpForce;
    35.             }
    36.         }
    37.         else
    38.         {
    39.             //Disable movement while in mid air
    40.             verticalVelocity -= gravity * Time.deltaTime;
    41.             moveVector = lastMove;
    42.         }
    44.         moveVector.y = 0;
    45.         moveVector.Normalize();
    46.         moveVector *= speed;
    47.         moveVector.y = verticalVelocity;
    49.         //Move player
    50.         controller.Move (moveVector * Time.deltaTime);
    51.         lastMove = moveVector;
    52.     }
    54.     {
    55.         //Detect if controller has jumped into a wall
    56.         if (!controller.isGrounded && hit.normal.y < 0.01f) {
    58.             //Apply wall slide
    59.             verticalVelocity = -1;
    62.             //Apply wall jump
    63.             if (Input.GetKeyDown (KeyCode.Space)) {
    64.                 verticalVelocity = jumpForce;
    65.                 moveVector = hit.normal * speed;
    66.             }
    68.             //Exit wall slide
    69.             if (Input.GetKeyDown (KeyCode.S)) {
    70.                 verticalVelocity = 0;
    71.                 moveVector.x = hit.normal.y;
    72.             }
    74.         } else
    75.             return;
    77.     }
    79. }

    Attached Files:

    • Bug.PNG
      File size:
      130.6 KB
  2. BrandyStarbrite


    Aug 4, 2013
    Do you have a trigger/collider, on the lower platform, that the red arrow
    is pointing too? If so......

    It's possible, that the trigger/collider (if there is one), on the lower platform
    might be a bit too wide, or the same size, as the lower platform.
    So when your character touches it, the game engine thinks, that you want
    to land, on that lower platform. That could explain, why you character
    gets pitched onto it.

    If the trigger/collider, is the same size and width as the lower platform....
    My suggestion would be, to move that lower platform, a bit to the left,
    away from being under, the wall above it. Then try to do, the wall slide
    thing and see what happens from there.

    Or, (if the trigger/collider is too wide), reduce the width size, of the
    trigger/collider and then (if you want) move the lower platform, a bit
    to the left, away from being under, the wall above it and see what happens
    from there.
    Last edited: May 11, 2018