Trouble Making New Levels

Hello,
I am on Complete Unity 2D Developer: Create Your Own 2D Games Using Unity C#, and I am section 5.32-Create New Levels. I duplicated my original level and creating the first level with only one room. However, after making the level and pressing play, my jumping doesn’t work, and there are many other bugs, such as sticking to the walls. I’m not sure what I did wrong. Please help me if possible.

Thanks,
Cork Emperor

I’m just guessing here, because you provided very little information as to what you did, but I’m assuming you deleted the layout of the level, if that’s the case, then your terrain doesn’t have the appropriate settings to work properly.

Check if your terrain’s colliders are in the correct Layer and have the correspondent physics material. Also check your player’s settings, like layer, colliders, that all the scripts have the correct references, and so on.

Hello Yee,
I realized that I did not do the right tilemap. However, after fixing it, my jumping still doesn’t work and the enemy movement does not work.

This is my jump code:
void OnJump(InputValue value)

{

    if (!isAlive) { return; }

    if(!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))

    {

    return;

    }

    if(value.isPressed)

    {

        myRigidbody.velocity += new Vector2(0f, jumpSpeed);

    }

}

This is my enemy code:
public class EnemyMovement : MonoBehaviour

{

[SerializeField] float moveSpeed = 1f;

Rigidbody2D myRigidBody;

 void Start()

{

    myRigidBody = GetComponent<Rigidbody2D>();

}

void Update()

{

    myRigidBody.velocity = new Vector2(moveSpeed, 0f);

}



void OnTriggerExit2D(Collider2D other)

{

    moveSpeed = -moveSpeed;

    FlipEnemyFacing();

}

void FlipEnemyFacing()

{

    transform.localScale = new Vector2(-(Mathf.Sign(myRigidBody.velocity.x)), 1f);

}

}

Thanks,
Cork Emperor

Hi Cork Emperor,

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?

In Rick’s game, OnJump gets called because of the user input. If OnJump doesn’t get called anymore in your new levels, please check if the PlayerInput component is still attached to the same game object as your script component. Also see here:
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.11/manual/PlayerInput.html#getting-started

The enemy movement depends on the value of moveSpeed, which is exposed in the Inspector due to the [SerializeField] attribute. If the value is 0 in the Inspector, the enemy won’t move. Of course, the EnemyMovement component must be attached to the enemy in your scene, and it must be active. Whether the relevant code gets called, can easily be checked with Debug.Log.

Did this help you fix the problems? :slight_smile:


See also:

Hello Nina,
My code is the same compared to the lecture. I added debug.logs to the OnJump method and it gets called in the console, but the actual jumping functionality doesn’t work. I think Yee is right about me not getting the physics material right, but I’m not sure. This is because everything works completely fine in my level 2 sample scene, but when I made a new level, it didn’t work anymore.

The PlayerInput component is also still attached to my game object. Also, my enemy is able to move, but its movement doesn’t work correctly. For example when it meets the edge of a wall, it walks right through it instead turning around the other way. Please give me more advice.

Thank you,
Cork Emperor

From what I see, there are 3 conditions in your OnJump method. Have you checked if all conditions have the expected value?

If one of them does not have the expected value, you could check why you get the wrong one. Maybe isAlive is false. We cannot know that just by reading the code.

Before the enemy reaches the wall, click the Pause button. Then zoom in on the enemy in the Scene window, so you can see its collider. Keep clicking the button that gets you to the next frame until the problem happens. Maybe you can spot an issue.

Rick’s concept is: If the periscope collider exists a collider (here: the ground collider), the enemy gets flipped.


In case it wasn’t clear: Unless you want to check every single detail in your game as suggested by Yee, which might take you a couple of hours, days or even weeks, you have to check if your current code follows the expected logic flow. By that, I don’t mean that something happens in the game window but that you get the expected values in your code. What happens in the game window is a result of your code, thus you need to check the code.

Hello Nina,
I believe that all the conditions have the expected because I put a debug.log in the third condition( if(value.isPressed)), and the debug.log would not print to the console if the conditions did not work.

Regarding the enemy, the flip works, but for some reason the enemy keeps moving in the same direction.

Do you believe that or did you verify that? The difference is crucial because it is easy to spend the next few days looking for the problem in the wrong place.

Regarding the enemy, the flip works, but for some reason the enemy keeps moving in the same direction.

Did you analyse the situation as described above? If so, did you see any unexpected behaviour like “enemy flipping multiple times” or something like that?

I suspect that the problem might be moveSpeed and myRigidBody.velocity.x, which are not synchronised and cannot be synchronised. Since I do not know what values you are getting at runtime, this is just a guess, though. For testing purposes, maybe try to flip the enemy based on moveSpeed instead of myRigidBody.velocity.x.

Hello Nina,
I was able to fix all the large errors by finessing the code. Instead of using moveSpeed = -moveSpeed for the enemy I just set the moveSpeed to -1 or 1 based on where the enemy was moving and it worked. For the player jumping I just used the old input system and it also worked.

Thank you for your help,
Cork Emperor

Fantastic! Is everything working now as expected? :slight_smile:

Yes the jumping is a little bit off but the game is functional, thank you for your help :grinning:

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

Privacy & Terms