If (myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemies", "Hazards")))

I still have the problem of not dying when I land on the Hazards, even if I have made the tile map C 2D to a trigger. How do I solve this?

Hi Ziqver,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? And have you already tried to add Debug.Logs to your code to see what is going on during runtime? If so, where did you place the Debug.Logs and what was the output in your console?

Screenshot 2022-07-16 at 16.24.09

When I arrive atProject Changes, it looks like this. Sorry, but I don’t know how to read this. Pic.1.
And this is my code. Pic2 and 3.


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.

Regarding GitLab, click on “View file”. See the top right corner of your first screenshot. At the very top, there should be a “Browse files” button or something that sounds similar. When you click on it, you should be able to see a normal folder/file structure. Just don’t click on the project name because that would show you the latest project state. You are interested in the state of this video, not a newer one.

Hope this helps :slight_smile:


See also;

Roger that, I will no longer screenshot my codes, from now on it is coy paste.
My codes are the same as in GitLab and I even checked the screenshots with the settings of Hazard Tilemap and the player. The same settings as the tutorial and screenshots. I don’t really see what the project changes is?
For me, nothing is new.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float runSpeed = 10f;
[SerializeField] float jumpSpeed =5f;
[SerializeField] float climbSpeed = 5f;
[SerializeField] Vector2 deathKick = new Vector2 (10f, 10f);
Vector2 moveInput;
Rigidbody2D myRigidbody;
Animator myAnimator;
CapsuleCollider2D myBodyCollider;
BoxCollider2D myFeetCollider;

 float gravityScaleAtStart;

 bool isAlive= true;


void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
     myAnimator = GetComponent<Animator>();
     myBodyCollider = GetComponent<CapsuleCollider2D>();
     myFeetCollider = GetComponent<BoxCollider2D>();
     gravityScaleAtStart = myRigidbody.gravityScale;
    
}


void Update()
{
    if (!isAlive) { return; }
    Run();
    FlipSprite();
    ClimbLadder();
    Die();
}

void OnMove(InputValue value)
{
    if (!isAlive) { return; }
    moveInput = value.Get<Vector2>();
    
}
 
 void OnJump(InputValue value)

 {
    if (!isAlive) { return; }
    if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }
   
 
    

    if(value.isPressed)
    {
        // do stuff
        myRigidbody.velocity += new Vector2 (0f, jumpSpeed);
    }
 }
void Run()
{
    Vector2 playerVelocity = new Vector2 (moveInput.x * runSpeed, myRigidbody.velocity.y);
    myRigidbody.velocity = playerVelocity;

    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;

    myAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
}

void FlipSprite()

{
    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;

    if (playerHasHorizontalSpeed)

    {
        transform.localScale = new Vector2 (Mathf.Sign(myRigidbody.velocity.x), 1f);
    }
}


void ClimbLadder()
{
    if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Climbing"))) 
    { 
        myRigidbody.gravityScale = gravityScaleAtStart;
        myAnimator.SetBool("isClimbing", false);
        return; 
    
    }


    Vector2 climbVelocity = new Vector2 (myRigidbody.velocity.x, moveInput.y * climbSpeed);
    
    myRigidbody.velocity = climbVelocity;
    myRigidbody.gravityScale = 0f;

     bool playerHasVerticalSpeed = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
    myAnimator.SetBool("isClimbing", playerHasVerticalSpeed);
}

void Die()
{
    if (myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemies", "Hazards")))
    {
        isAlive = false;
        myAnimator.SetTrigger("Dying");
        myRigidbody.velocity = deathKick;  
    }
    
}

}

Thank you. Code as text is really helpful. :slight_smile:

Add two Debug.Log to the Die() method: One outside the if-block, and on inside the if-block.

Which message(s) got logged into your console at runtime?

If none got logged, check where the Die() method gets called. If the Debug.Log in the if-block did not log a message into your console, check the spelling of your masks in the Unity Editor. Make sure there are no space characters like " Hazards" or "Hazards ". The hazards game object must be assigned to the “Hazards” layer, and they need a collider attached.

Thanks for the reply Nina!
Like this?

void Die()
{
Debug.Log;
if (myBodyCollider.IsTouchingLayers(LayerMask.GetMask(“Enemies”, “Hazard”)))
Debug.Log;
{
isAlive = false;
myAnimator.SetTrigger(“Dying”);
myRigidbody.velocity = deathKick;

Like we used Debug.Logs in previous projects in this course: For example:

Debug.Log("This is a meaningful message.");

And the syntax must also be correct.

Yes, sorry. That was what I meant. Just wanted to know if the position of Debug.Log (“This is a meaningful message.”); is in the correct space in the code.
The message I get is " die on Hazards
UnityEngine.Debug:Log (object)
PlayerMovement:Die () (at Assets/Scripts/PlayerMovement.cs:109)
PlayerMovement:Update () (at Assets/Scripts/PlayerMovement.cs:39)
"
But the player dies right away when I press play and flies to the left of the stream until he hits a wall. Then he just lies there, lifeless.

Sorry Nina, I am pretty sure I double c

Sorry Nina, I am pretty sure I double checked. But I must have not. The Hazards sprite was not in Hazards Tilemap, it was on Platform. Now it works just fine and it si solved. All The Best, Serafín.

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

Privacy & Terms