Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. 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
  6. 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.  
    14.     private void Start()
    15.     {
    16.         //Get Character Controller
    17.         controller = GetComponent<CharacterController> ();
    18.     }
    19.  
    20.     private void Update()
    21.     {
    22.    
    23.         //Set movement vectors
    24.         moveVector = Vector3.zero;
    25.         moveVector.x = Input.GetAxis ("Horizontal") * speed;
    26.  
    27.         //Apply jump if controller is on the ground
    28.         if (controller.isGrounded)
    29.         {
    30.             verticalVelocity = -1;
    31.  
    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.         }
    43.  
    44.         moveVector.y = 0;
    45.         moveVector.Normalize();
    46.         moveVector *= speed;
    47.         moveVector.y = verticalVelocity;
    48.  
    49.         //Move player
    50.         controller.Move (moveVector * Time.deltaTime);
    51.         lastMove = moveVector;
    52.     }
    53.  
    54.     {
    55.         //Detect if controller has jumped into a wall
    56.         if (!controller.isGrounded && hit.normal.y < 0.01f) {
    57.  
    58.             //Apply wall slide
    59.             verticalVelocity = -1;
    60.  
    61.  
    62.             //Apply wall jump
    63.             if (Input.GetKeyDown (KeyCode.Space)) {
    64.                 verticalVelocity = jumpForce;
    65.                 moveVector = hit.normal * speed;
    66.             }
    67.            
    68.             //Exit wall slide
    69.             if (Input.GetKeyDown (KeyCode.S)) {
    70.                 verticalVelocity = 0;
    71.                 moveVector.x = hit.normal.y;
    72.             }
    73.            
    74.         } else
    75.             return;
    76.  
    77.     }
    78.    
    79. }
     

    Attached Files:

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

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    804
    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