About 'Making Enemies'!

In this video (objectives)…

  1. Import enemy sprite and set up required components.
  2. Script a way for the enemy to flip when it reaches the end of a platform.

After watching (learning outcomes)…

Create an enemy that moves along platforms, turning when it reaches the end.

(Unique Video Reference: 22_TV_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

For me this video jumps straight into the ‘Show Level Load Progress Bar’ video of the next section for some reason.

Same for me

Thanks for highlighting this, it looks like there was an error with the upload on Udemy’s side, I’m investigating and looking to fix it.

2 Likes

…ironic title for the lecture @Rick_Davidson :smiley:

1 Like

Hi all, just wanted to share how I solved enemy flipping and changing direction in my code:

private void OnTriggerExit2D(Collider2D collision)
{
    // move box collider to opposite side
    _wallCheckCollider.offset = new Vector2(-_wallCheckCollider.offset.x, _wallCheckCollider.offset.y);
    // flip using sprite renderer
    _sr.flipX = !_sr.flipX;
    // change speed sign
    _moveSpeed = -_moveSpeed;
}

I’ve been coding my solution in a entirely different way, so my enemy has access to the same movement controls as the player.
I figured that since my enemy had the ability to jump if I wanted, I might as well see what happens if I let him jump when he hits a wall. Turns out the little critter figured out how to get up stairs!

2 Likes

Awesome, thats really cool.

1 Like

My own solution… But I am confused - The way @Rick_Davidson does it, whilst I understand we are supposed to be learning (and I went back and also tried implementing Rick’s version and did learn a thing or two with implementing returning a bool in this way), does seem to over-complicate this whole process?
At least from the perspective of a relatively untrained eye… and brain perhaps? :sweat_smile:

Learning outcomes aside, is there any benefit to doing it the “long-winded way” or is it really just a case of “there’s more than one way to skin a cat”, and I guess some ways take a little bit longer and take a bit more space up than others?

void Update ()
{
    rb.velocity = new Vector2(moveSpeed, 0f);
}

private void OnTriggerExit2D(Collider2D collision)
{
    transform.localScale = new Vector2(-transform.localScale.x, 1f);
    moveSpeed = -moveSpeed;
}

Edit: What I mean is, ultimately, is there anything wrong with the way I have done it? I mean it works, but seemed a lot easier.

For some reason (when my code appears to be the same, but maybe I’m having a “boys’ look”) my enemy will only swap directions once. Anyone else having this issue?

Edit: Now it appears to work twice sometimes, not at all other times. I think it’s got something to do with whether there is a ledge on one side, or a wall?

I really don’t understand the OnTriggerExit2D part either. What exactly is exiting the trigger?

Hey Cam, its a little bit counter intuitive what is happening, particularly when we use the world “volume” to refer to collisions, however the OnTriggerExit2D is really only taking into consideration the green collision line itself, not the volume that is represented inside the area.

The box collider on the enemy starts underground and you can see there is an intersection between the green lines of the box, and the green lines of the ground plan. When an enemy gets to the end of a platform, or to a wall, there will be a moment when there are no longer green lines of colliders touching each other - its at this point that the OnTriggerExit2D determines that its condition is true.

Hope this gives you some additional intel to help.

3 Likes

Thanks for explaining that, Rick. You’re right, I find that totally unintuitive but now I understand what it’s doing at least.

In terms of my flipping conundrum, I figured that out too and it was my fault (but may pose problems for flexibility down the track). I had kept my Rigidbody2D Body Type to Dynamic so that it could sit flush to the platform without me needing to meticulously place it (or make it drop off in other, later enemy types). Switching it back to Kinematic (like you had) fixed the issue.

Glad you resolved it.

We also wrestled with the idea of dynamic so that it sat flush, but I’ve found that hand-placing hasn’t been too bad. Doesn’t need to be pixel perfect in this instance.

Awesome, I got this working but I couldn’t get my head around the TriggerExit… My thoughts were

“Surely the collide should fire an exit every time it leaves a block”
So its not triggering on the leaving of each block so…
“But the collide is always in a ‘Foreground’ mass and never exits”
WTF?

So its the actual green line its checking, not the mass. So glad this was brought up… my head was popping.

I’m curious to know this too. I got it to work pretty much like you but a little different.

// Update is called once per frame
void Update () {
    myRigidBody.velocity = new Vector2(moveSpeed, 0f);
}

private void OnTriggerExit2D(Collider2D collision)
{
    moveSpeed *= -1;

    // reverse the current scaling of x-axis
    this.transform.localScale = new Vector2(Mathf.Sign(moveSpeed), 1f);
}

For those who are having problems with their Enemy clipping through part of the wall before flipping (or maybe not flipping at all), I recommend updating your Unity version to 2018.2 Beta.

The cause was that the mirrored Rule Tiles didn’t quite connect with the existing Composite Collider 2D. This was reported as a bug and in the report it said that it’s now been fixed as of Unity 2018.2.

Ah good, thanks for letting us know about this.

void Start(){
	myRigidbody = GetComponent<Rigidbody2D>();
	myRigidbody.velocity = new Vector2(speed,0f);
}

void OnTriggerExit2D(Collider2D col){
	myRigidbody.velocity = -myRigidbody.velocity;
	transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), transform.localScale.y);
}

I did it like this, which I wondering, would it run quicker than the lecture’s solution, as it doesn’t need to do as many calculations?

If it works for you and is simpler, then awesome, run with it!

Privacy & Terms