Able to Jump either on Ground or Climbing

I am attempting to allow my player to be able to Jump either while they are on the ground or while they are on the ladder.

I have tried to use both the | operator and the ^ operator.
When I do it with either operator I disable jumping entirely.

I have two questions:

  1. What is exactly the difference between | and ^ ?
  2. What would be the easiest way to alter this current code to allow that function for the player?

My script is otherwise exactly as Ricks in the lecture.

Hi,

  1. ^ is the logical exclusive OR operator, | the logical OR operator. See here. In our course, we use neither ^ nor |, and you rarely see those in simple if-conditions like this elsewhere. Instead, we use || and &&.

  2. Did you also test ||? And have you already looked the GetMask method up in the Unity API?

I hope this helped. :slight_smile:


See also:

So I looked into the API and noticed that GetMask has array capabilities. I’m not sure how an array would be able to check one or the other, but I tried it out with the following code:

This allowed you to still jump on the ground, but not while on the ladder/climbing layer. I thought it might be fixed by removing the 0ing out of gravity we implement to fix the sliding. This merely resulted in sliding down the ladder again while still unable to jump off the ladder.

Doing this breaks the ability to jump entirely.

I then tried to do this since the method is IsTouchingLayerS (I know the S isn’t supposed to be capital but I noticed that it is plural). I first tried using || in the same code and got an error that || can’t be used to compare two ints. So I tried the other OR operators. ^ broke the code. How I have it depicted in the screenshot above I was able to again jump on the ground but not on the ladder/climbing layer.

I have done the things that make sense and have tried to think it through but am unable to find a solution.

I’m wondering what the solution would be, if you do provide a solution if you could also explain why that works I would grately appreciate that.

Sincerely,
Drakken

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

That’s odd. IsTouchingLayers returns a bool, not an int. If you placed || between the two method calls, that should have worked because the condition was supposed to check if one of the returned values is true.

For the version with the |, it might be an idea to log the whole expression into your console by with I mean the value you pass on to IsTouchingLayers, not the value returned by IsTouchingLayers. LayerMask.GetMask returns an int.

If you still cannot find any solution, log the name of the layer your player is touching at runtime into your console. Maybe your player does not touch the expected layers. Or maybe a game object was assigned to the wrong layer. Or maybe there is a typo in the ‘Climbing’ layer name in Unity. Take a very close look at the spelling, and check if there are any spaces such as in 'Climbing '.

What are the coding fencing characters?

How would I log at runtime the layer my player is touching?

What do you mean by that?

How would I log at runtime the layer my player is touching?

Have you already seen this thread?

Also see the API:

I’m unsure as to what the code fencing characters are.

Thanks you for the articles hopefully I am able to derive some solution from it

What are code fencing characters? Could you give me an example? Or describe what the problem is that you’d like to solve via code?

Thanks you for the articles hopefully I am able to derive some solution from it

I’m sure you will find a solution. Your code makes sense, so you are definitely on the right track. There is very likely a ‘problem’ in Unity. Figure out what’s going on at runtime, and you’ll figure out why your code does not work as expected.

You told me as per the quote in the last message to copy and paste my code and use code fencing characters vs taking screenshots. I have 0 idea what they are and thus am asking you, since you mentioned them in the first place.

I took a look at those articles and they didn’t really make sense with what I’m trying to do. I’m sure they would to a more experienced coder but thats not me, yet. I’ll probably just move on with the course and see if I come back to it later or something. I appreciate the help even though the solution didn’t really happen

Dear me, you are right! Sorry for the misunderstanding in my side and the confusion. I pasted an instruction another teaching assistant created years ago. In all those years, nobody has ever asked me what “code fencing characters” are. They are this:

image

Unfortunately, I cannot type them here because the text gets formatted as code. If you use the characters three times on each side, you can apply the code formatting to multiple lines of code.

You could also highlight your code and click this button:

image

I hope this makes sense now. :slight_smile:


One more thing you could check: In Rick’s original code, we override the velocity in some of the methods. Check all movement methods (jumping, climbing, moving, and so on) whether your code applies a hard-coded value or a value without taking the current value into consideration. It might be that your OnJump method works perfectly fine but other code might override the values you applied in OnJump. Use Debug.Log to see what’s going on at runtime. That’s what a more experienced programmer would do as well because nobody can see the values that are generated at runtime.

Look for = vs. += in the movement methods.

Okay yeah no problem that does make sense.

I looked into it a little more and it seems that the climbladder() method hard codes the Y velocity and the onJump() is trying to alter the Y velocity thats already being altered. So in order to allow jumping on the ladder I would need to completely rework all the functions. I’ll probably save that for another go around and just build my levels with that in mind. Thank you so much for the help, it is truely appreciated.

The onJump uses += but the climbladder uses =. Would changing the climbladder to += theoretically solve the problem?

Test your idea! :slight_smile:

thanks I’ve moved past that for now didn’t want to be stuck on something for too long when I can come back later

That’s probably a good idea because your code could become more complicated, and implementing the rest of Rick’s code could become difficult… Once you completed the game, you could try to make your idea work again. Then you won’t have to worry about Rick’s code. :slight_smile:

Privacy & Terms