Tilevainia player won't climb ladder

No Climb Ladder
,…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Player : MonoBehaviour
{ /float controlPlayer = Input.GetAxis(“Horizontal”);/
//config
[SerializeField] float runSpeed = 5f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float climbSpeed = 5f;
//State
bool isAlive = true;
Rigidbody2D myRigidBody;
Animator myAnimator;
Collider2D myCollider2D;

void Start()
    
{
    myRigidBody = GetComponent<Rigidbody2D>();
    myAnimator = GetComponent<Animator>();
    myCollider2D = GetComponent<Collider2D>();
}

// Update is called once per frame
void Update()
{
    Run();
    FlipSprite();
    Jump();
    ClimbLadder();
  
}
private void Run()
{
    float controlThrow = CrossPlatformInputManager.GetAxis("Horizontal"); // value is betweeen -1 to +1
    Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
    myRigidBody.velocity = playerVelocity;

//makes idle work by playerHasHorizontalSpeed if there is any or not makes the true or false values
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool(“Running”, playerHasHorizontalSpeed);

}

private void ClimbLadder()
{
    if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Climbing"))) { return; }
    float controlThrow = CrossPlatformInputManager.GetAxis("Vertical");
    Vector2 climbVelocity = new Vector2(myRigidBody.velocity.x, controlThrow * climbSpeed);
    myRigidBody.velocity = climbVelocity;
}

private void Jump()
{
    //if the condition is not met then don't let the player jump
    if(!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))
    { return; }
    if (CrossPlatformInputManager.GetButtonDown("Jump"))
    {
        Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
        myRigidBody.velocity += jumpVelocityToAdd;
    }


    //if player is moving horizontally reverse the current scaling of the X asis

}

private void FlipSprite()
{
    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
    if (playerHasHorizontalSpeed)
    {
        transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
    }

    }

}
…,
I have the climbing ladder on Climbing sorting layer & layer (user 9). I think it’s because in 2017.3 the grid options are less then in 2020 so I have an idea. Can anyone assist or just tell me why my player is ignoring the ladder? Yes the ladder/climbing is trigger.

Hi,

Does the ladder game object have the “Climbing” layer assigned? Does the ladder have got a collider? Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Have you already rewatched the video at least one more time?


See also:

Yes Ma’am my Climbing gameobject has a layer & a sorting layer of Climbing. I’ve compared my code with Rick & if I used his code I’d get errors. I don’t have a “myfeet” I assume he discusses that later on in this course. I’ve watched the video at least 10 times & a few times with my friend Jimmy who is also taking this course. I know he watched it at least 3x when we hung out lastnight & tried to figure this out. We worked for about 2 hours together & couldn’t figure it out.
Oops did I not format the code right sorry.

If you mean the link in udemy that says lecture project changes…none of those links has ever worked from udemy. Ok well the download file links work but any link Rick has to another webpage has never worked. So I guess the answer to that question is no if that’s what you’re referring to.

Did you click the left mouse button? The right mouse button does not work on Udemy. And in which exact lecture are you? What is its name? And what is the exact name of the section? Which version of Unity do you use?

And could you please share a screenshot where I can see the collider of the ladder?

Add Debug.Logs to your code to see what is happening during runtime. For example, it could be interesting to see where ClimbLadder() gets executed. Maybe if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Climbing"))) returns true.



Complete C# Unity Game Developer 2D
Lesson # 329 “Climb Ladder”
Yes I click left on the mouse button. I know how to follow a link. I thought you were trying to tell me to try right clicking but that didn’t work either.
Unity 2020.
Ok I’m going to try to see if Debug.Log will actually work this time because it has not in a while.
I don’t know if you see what I was talking about with the grids but I tried every possible angle me & Jimmy could.
Thank you.

Add a debug log under

Debug.Log(“Test”);

Is that showing up when you hit the ladder? If not, then it’s probably something to do with your colliders and how they are set up. Everything else in the code itself looks OK to me…(Although you do have a null reference error as well in your console that needs addressing).

I noticed that you added a second tilemap ladder layer – the second ladder layer doesn’t have any colliders on it - it needs to have them, same as the parent layer, if you have any sprites on it. That could be your issue.

And there is a NullReferenceException error message in your console, maybe even more errors. Fix it/them first because they might be related to your problem.

1 Like

As usual when I use Debug.Log nothing happens.
The null reference is gone when I hit the play button.
The reason I have a 2nd layer on the tilemap is because I had no choice. I tried to make it one layer but unity didn’t allow me. I went through all 5 tile map options & did what Rick said on all 5 options to the best I could but again I was not allowed by unity to make it any other way.
I do realize that is more then likely why I’m having problems but after trying hours (with help) to solve this problem I have no clue what to do.
Thank you.

Ok so the reason nothing is happening with Debug.Log is because it is returning, because it isn’t touching a collider in the Climbing layer. That’s why you use it – it’s just as useful when it doesn’t show up, as when it does. :slight_smile:

I think you just need to add the colliders to the second layer you had to make. So do the same process as Rick does when he adds the colliders.

You can definitely get it working on one layer, but it doesn’t really matter as long as you put the colliders there. Otherwise even if you have the correct Layer, Unity still won’t pick up that there is any collision.

Let us know if that fixes it for you!

I’m going to try that. What I really wish we could figure out is how to make it 1 layer like the other 2 tilemaps.

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.GameObjectInspector.ClearPreviewCache () (at <50f55621a2ca4f31a35283e2979a8bf5>:0)
UnityEditor.GameObjectInspector.ReloadPreviewInstances () (at <50f55621a2ca4f31a35283e2979a8bf5>:0)
UnityEditor.GameObjectInspector.OnForceReloadInspector () (at <50f55621a2ca4f31a35283e2979a8bf5>:0)
UnityEditor.Tilemaps.GridPaintPaletteClipboard:OnDisable() (at E:/Program Files/Unity/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.2d.tilemap/Editor/GridPaintPaletteClipboard.cs:347)
The null reference is back & I’d paste the code but it’s 1122 lines long.
Something is telling me I should not mess with this code in any way.


Also TYVM my guy is walking up & down the ladder the wrong way but Rick explains how to fix that in the video.

So I turned down the climb speed & the null reference disappeared.
Thanks.
I’m really sorry but I was wondering since Rick is using “Edit Physics Shape” & that’s not available in unity 2020 & the “Custom Physics Shape” don’t work like “Edit Physics Shape” I assumed the closest we can get is the sprite editor which would give me a picture like this but I can still climb the ladders from the sides & even jump a little if I’m on top.

Nevermind I read the QnA & got it.
Thank you.

Is the problem solved, @davidwindsands? :slight_smile:


See also:

Sorry about that miss Nina. Tilevainia is such an exciting course I forgot to hit solution.
Thank you Nina & heckadactyl for your assistance.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms