Hello)
I do my first game on Unity. 2D platformer.
When writing a code for jumping from the wall faced a problem. Everything works, but the jump is performed vertically up, although it must be repelled from the wall.
At first, the prelation of x occurred using input.getaxisraw ("horizontal")
, but learned that it is better to track the movement of the condition. Instead of Velocity, addForce applied, commented on the sections of the code that theoretically could conflict. No results. True, in some cases, the character once out of 10-20 jumps teleported to the distance from the wall.
I am in advance for the quality of code
Public Class Playercontroller: Monobehaviour
{
Public Float MaxSpeed;
Public Float JumpForse;
Private Rigidbody2d Therb;
Public Transform GroundCheckPoint;
Public Layermask Whatisground;
Private Bool IsGrounded;
Private Animator Anim;
Public Spriterenderer Playersr;
Public Float Hangtime = .2f;
Private Float Hangcounter;
Public float jumpbufferlength = .03f;
Private Float JumpBuffercount;
Private Bool IsfacingRight = True;
Public Float HangtimeForwall = .2f;
Private Float HangcounterForwall;
Public float walljumpbufferlength = .03f;
Private float walljumpbuffercount;
Public Float WallJumpSpeedX;
Public Float WallJumpForse;
Private Float WallJumpfaceDirection = -1;
Public Bool Isstouchingwall;
Public Transform Wallcheck;
Public Float WallCheckdistance;
Private Bool iSwallsliding;
Public float iswallslidingSpeed;
Void Start ()
{
therb = getComponent & lt; rigidbody2d & gt; ();
}
Void Update ()
{
CheckmovementDirection ();
// Check, on Earth Player
isgrounded = physics2d.overlapcircle (Groundcheckpoint.position, .2f, Whatisground);
Checkifwallsliding ();
// managehangtime
If (ISGrounded)
{
hangcounter = hangtime;
} ELSE.
{
hangcounter - = time.deltatime;
}
// Buffer Jumps
if (Input.getButtondown ("Jump"))
{
jumpbuffercount = jumpbufferlength;
}
ELSE.
{
jumpbuffercount - = time.deltatime;
}
// Jump
If (jumpbuffercount & gt; = 0 & amp; & amp; hangcounter & gt; 0f & amp; & amp;! iswallsliding)
{
Therb.Velocity = New Vector2 (Therb.Velocity.x, JumpForse);
jumpbuffercount = 0;
}
// Slide on the wall and jump from the wall
iStouchingwall = physics2d.raycast (wallcheck.position, transform.right, wallcheckdistance, whatisground);
If (iSwallsliding)
{
If (Therb.Velocity.y & lt; -iswallslidingSpeed)
{
Therb.Velocity = New Vector2 (Therb.Velocity.x, -iswallslidingSpeed);
}
}
If (iSwallsliding)
{
HangcounterForwall = HangtimeForwall;
}
ELSE.
{
HangcounterForwall - = time.deltatime;
}
// buffer jumping from the wall
if (Input.getButtondown ("Jump"))
{
WallJumpBuffCount = WallJumpBufferLength;
}
ELSE.
{
WallJumpBuffCount - = time.deltatime;
}
// !!!!!!!!!!!!!!!!!! This is the leap itself from the wall !!!!!!!!!!!!!!!!!!!!!!!!! !!!!!
If (WallJumpBufferCount & GT; = 0 & amp; & amp; hangcounterforwall & gt; 0f)
{
// this way I tried first
//Therb.Velocity = New Vector2 (Therb.Velocity.x * WallJUMPSPEEDX * WallJumpfaceDirection, WallJUMPFORSE);
Vector2 ForceToadd = New Vector2 (Therb.Velocity.x * WallJUMPSPEEDX * WallJumpFaceDirection, WallJUMPFORSE);
Therb.addForce (ForceToadd, Forcemode2D.impulse);
walljumpbuffercount = 0;
}
}
Private Void CheckmovementDirection ()
{
if (isfacingRight & amp; & amp; input.getkey ("a"))
{
Flip ();
}
ELSE If (! isfacingRight & amp; & amp; input.getKey ("D"))
{
Flip ();
}
}
// Uzorot
Private Void Flip ()
{
If (! ISWALLSLIDING)
{
isfacingRight =! IsfacingRight;
transform.rotate (0.0F, 180.0F, 0.0F);
walljumpfacedirection * = -1;
}
}
// Check on a slide on the wall
Private void checkifwallsliding ()
{
If (iStouchingwall & amp; & amp; ISGrounded & amp; & amp; therb.velocity.y & lt; 0)
{
ISWALLSLIDING = TRUE;
}
ELSE.
{
iSwallsliding = false;
}
}
Private Void FixedUpdate ()
{
//Run
if (input.getKey ("D"))
{
Therb.Velocity = New Vector2 (MaxSpeed, Therb.Velocity.y);
}
ELSE If (Input.getKey ("A"))
{
Therb.Velocity = New Vector2 (-MaxSpeed, Therb.Velocity.y);
}
ELSE.
{
Therb.Velocity = New Vector2 (0, Therb.Velocity.y);
}
// Slowing Character at Peak Jump
If (! isgrounded & amp; & lt; 10 & amp; & amp; therb.velocity.y & gt; -10)
{
Therb.gravityScale = 6;
}
ELSE.
{
Therb.gravityScale = 10;
}
If (! isgrounded)
{
MaxSpeed = 10;
}
ELSE.
{
MaxSpeed = 8;
}
}
}
Answer 1, Authority 100%
With a debug, I cannot be controlled yet, but it’s possible to comment all the sections of the code in turn. In short, the problem was that the code of the code with running conflict with the jump from the wall. If you make a jump from the wall and running using AddForce, then everything works, but I would not want to leave the move via AddForce. Too uncontrollable character turns out.