Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Platformer Microgame Wall Jumping

Discussion in '2D' started by EmberGalaxy, Dec 28, 2019.

  1. EmberGalaxy

    EmberGalaxy

    Joined:
    Dec 28, 2019
    Posts:
    1
    Hi, so I don't know coding, and I just started using Unity. I'm making a platformer with the Platformer Microgame. So far I have changed textures, audio, controls and other things. Now I want to add some physics to the character. How can I make him wall jump? I guess I would have to edit the script, but I don't know C# and tutorials on YouTube don't really use the same jump script, so I'm assuming the wall jump would be for their jump. Any help appreciated. Here's the Player Controller script (didn't change anything except for Player speed and jump take off speed:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Platformer.Gameplay;
    5. using static Platformer.Core.Simulation;
    6. using Platformer.Model;
    7. using Platformer.Core;
    8.  
    9. namespace Platformer.Mechanics
    10. {
    11.     /// <summary>
    12.     /// This is the main class used to implement control of the player.
    13.     /// It is a superset of the AnimationController class, but is inlined to allow for any kind of customisation.
    14.     /// </summary>
    15.     public class PlayerController : KinematicObject
    16.     {
    17.         public AudioClip jumpAudio;
    18.         public AudioClip respawnAudio;
    19.         public AudioClip ouchAudio;
    20.  
    21.         /// <summary>
    22.         /// Max horizontal speed of the player.
    23.         /// </summary>
    24.         public float maxSpeed = 7;
    25.         /// <summary>
    26.         /// Initial jump velocity at the start of a jump.
    27.         /// </summary>
    28.         public float jumpTakeOffSpeed = 7;
    29.  
    30.         public JumpState jumpState = JumpState.Grounded;
    31.         private bool stopJump;
    32.         /*internal new*/ public Collider2D collider2d;
    33.         /*internal new*/ public AudioSource audioSource;
    34.         public Health health;
    35.         public bool controlEnabled = true;
    36.  
    37.         bool jump;
    38.         Vector2 move;
    39.         SpriteRenderer spriteRenderer;
    40.         internal Animator animator;
    41.         readonly PlatformerModel model = Simulation.GetModel<PlatformerModel>();
    42.  
    43.         public Bounds Bounds => collider2d.bounds;
    44.  
    45.         void Awake()
    46.         {
    47.             health = GetComponent<Health>();
    48.             audioSource = GetComponent<AudioSource>();
    49.             collider2d = GetComponent<Collider2D>();
    50.             spriteRenderer = GetComponent<SpriteRenderer>();
    51.             animator = GetComponent<Animator>();
    52.         }
    53.  
    54.         protected override void Update()
    55.         {
    56.             if (controlEnabled)
    57.             {
    58.                 move.x = Input.GetAxis("Horizontal");
    59.                 if (jumpState == JumpState.Grounded && Input.GetButtonDown("Jump"))
    60.                     jumpState = JumpState.PrepareToJump;
    61.                 else if (Input.GetButtonUp("Jump"))
    62.                 {
    63.                     stopJump = true;
    64.                     Schedule<PlayerStopJump>().player = this;
    65.                 }
    66.             }
    67.             else
    68.             {
    69.                 move.x = 0;
    70.             }
    71.             UpdateJumpState();
    72.             base.Update();
    73.         }
    74.  
    75.         void UpdateJumpState()
    76.         {
    77.             jump = false;
    78.             switch (jumpState)
    79.             {
    80.                 case JumpState.PrepareToJump:
    81.                     jumpState = JumpState.Jumping;
    82.                     jump = true;
    83.                     stopJump = false;
    84.                     break;
    85.                 case JumpState.Jumping:
    86.                     if (!IsGrounded)
    87.                     {
    88.                         Schedule<PlayerJumped>().player = this;
    89.                         jumpState = JumpState.InFlight;
    90.                     }
    91.                     break;
    92.                 case JumpState.InFlight:
    93.                     if (IsGrounded)
    94.                     {
    95.                         Schedule<PlayerLanded>().player = this;
    96.                         jumpState = JumpState.Landed;
    97.                     }
    98.                     break;
    99.                 case JumpState.Landed:
    100.                     jumpState = JumpState.Grounded;
    101.                     break;
    102.             }
    103.         }
    104.  
    105.         protected override void ComputeVelocity()
    106.         {
    107.             if (jump && IsGrounded)
    108.             {
    109.                 velocity.y = jumpTakeOffSpeed * model.jumpModifier;
    110.                 jump = false;
    111.             }
    112.             else if (stopJump)
    113.             {
    114.                 stopJump = false;
    115.                 if (velocity.y > 0)
    116.                 {
    117.                     velocity.y = velocity.y * model.jumpDeceleration;
    118.                 }
    119.             }
    120.  
    121.             if (move.x > 0.01f)
    122.                 spriteRenderer.flipX = false;
    123.             else if (move.x < -0.01f)
    124.                 spriteRenderer.flipX = true;
    125.  
    126.             animator.SetBool("grounded", IsGrounded);
    127.             animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
    128.  
    129.             targetVelocity = move * maxSpeed;
    130.         }
    131.  
    132.         public enum JumpState
    133.         {
    134.             Grounded,
    135.             PrepareToJump,
    136.             Jumping,
    137.             InFlight,
    138.             Landed
    139.         }
    140.     }
    141. }
     
  2. Pavol19

    Pavol19

    Joined:
    Nov 25, 2020
    Posts:
    2
    Man i'm sorry i don't have the answer for your question, but i think you can help me, how exactly did you change the controls?