Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. 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

    jasperkeithfoster

    Joined:
    Apr 11, 2018
    Posts:
    1
    Hello,
    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;
    3.  
    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;
    13.     private CharacterController controller;
    14.  
    15.     private void Start()
    16.     {
    17.         //Get Character Controller
    18.         controller = GetComponent<CharacterController> ();
    19.     }
    20.  
    21.     private void Update()
    22.     {
    23.    
    24.         //Set movement vectors
    25.         moveVector = Vector3.zero;
    26.         moveVector.x = Input.GetAxis ("Horizontal") * speed;
    27.  
    28.         //Apply jump if controller is on the ground
    29.         if (controller.isGrounded)
    30.         {
    31.             verticalVelocity = -1;
    32.  
    33.             if (Input.GetKeyDown (KeyCode.Space))
    34.             {
    35.                 verticalVelocity = jumpForce;
    36.             }
    37.         }
    38.         else
    39.         {
    40.             //Disable movement while in mid air
    41.             verticalVelocity -= gravity * Time.deltaTime;
    42.             moveVector = lastMove;
    43.         }
    44.  
    45.         moveVector.y = 0;
    46.         moveVector.Normalize();
    47.         moveVector *= speed;
    48.         moveVector.y = verticalVelocity;
    49.  
    50.         //Move player
    51.         controller.Move (moveVector * Time.deltaTime);
    52.         lastMove = moveVector;
    53.     }
    54.  
    55.     private void OnControllerColliderHit(ControllerColliderHit hit)
    56.     {
    57.         //Detect if controller has jumped into a wall
    58.         if (!controller.isGrounded && hit.normal.y < 0.01f) {
    59.  
    60.             //Apply wall slide
    61.             verticalVelocity = -1;
    62.  
    63.  
    64.             //Apply wall jump
    65.             if (Input.GetKeyDown (KeyCode.Space)) {
    66.                 verticalVelocity = jumpForce;
    67.                 moveVector = hit.normal * speed;
    68.             }
    69.            
    70.             //Exit wall slide
    71.             if (Input.GetKeyDown (KeyCode.S)) {
    72.                 verticalVelocity = 0;
    73.                 moveVector.x = hit.normal.y;
    74.             }
    75.            
    76.         } else
    77.             return;
    78.  
    79.     }
    80.    
    81. }
     

    Attached Files:

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

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    986
    Okay.
    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