Hi All
I implemented the code from lecture 86. Climb That Ladder but something weird happened when I’m touching the ladder
the speed of my character is changing and when I’m trying to go up the ladder
my character flying up
Hi All
I implemented the code from lecture 86. Climb That Ladder but something weird happened when I’m touching the ladder
the speed of my character is changing and when I’m trying to go up the ladder
my character flying up
Hi kodesh,
Welcome to our community!
Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? If so, lave you also tried to add Debug.Logs to your code to see what is going on during runtime?
Maybe the player gets too much velocity on the y-axis.
I compared the code and it looks similar, to the video
I’m using different sprits but that should not affect the player’s movement
the only solution I found that helps not to fly out of the map is to decrease the climbSpeed to 0.005
but with climbSpeed at 0.005, the player slid up and down the ladder
all the rest values are the same as in the lecturer
How do you make the player climb? Do you use Time.deltaTime
?
No
I don’t use Time.deltaTime in any place in the script
I wrote the script in a different way but changed it to be exactly like in the lecture when I was trying to solve the issue
this is the video of the issue
and this is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovment : MonoBehaviour
{
Vector2 moveInput;
Rigidbody2D rBody2d;
CapsuleCollider2D myCollider2D;
Animator myAnimator;
[SerializeField] float moveSpeed = 20;
[SerializeField] float jumpSpeed = 40f;
[SerializeField] float climbSpeed = 0.05f;
float startGravity;
void Awake()
{
rBody2d = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
myCollider2D = GetComponent<CapsuleCollider2D>();
startGravity = rBody2d.gravityScale;
}
void Update()
{
Run();
bool isWalk = FlipSprite();
myAnimator.SetBool("isRunning", isWalk); ;
ClimbLadder();
}
void OnMove(InputValue val)
{
moveInput = val.Get<Vector2>();
}
void OnJump(InputValue val)
{
if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground"))) return;
if (val.isPressed) rBody2d.velocity += new Vector2(0f, jumpSpeed);
}
void Run()
{
Vector2 playerVelocity = new Vector2(moveInput.x * moveSpeed, rBody2d.velocity.y);
rBody2d.velocity = playerVelocity;
}
bool FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(rBody2d.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(rBody2d.velocity.x), 1f);
}
return playerHasHorizontalSpeed;
}
void ClimbLadder()
{
if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Ladder")))
{
rBody2d.gravityScale = startGravity;
myAnimator.SetBool("isCliming", false);
return;
}
rBody2d.velocity += new Vector2(rBody2d.velocity.x, moveInput.y * climbSpeed);
Debug.Log("moveInput.y: " + moveInput.y);
Debug.Log("climbSpeed: " + climbSpeed);
rBody2d.gravityScale = 0;
myAnimator.SetBool("isCliming", true);
}
}
do you see anything I need to change?
also, please notice I’m changing the gravity to 0 when I’m climbing
and for climbing I’m using only 1 PNG (i don’t have animation for the back of the palyer,a nd I drew the hair manually )
I’m not sure about this line. Since the velocity
is not clamped, it increases while the ‘climb’ key is pressed down. Test =
instead of +=
.
The player sprite looks good.
Yes!
now it’s working
Thanks!
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.