Sorry about the quality! For some reason it exported at 720 instead of 1080p and It was too later before I noticed! Hopefully you can still learn something from the video!
I love this series! You explain things fast, clear, and correctly! Thank you so much. It was exactly what I was looking for. Like everything I needed to put in my game you explained to me. Thanks again!!!
Hi there. My Patrolling Enemy won't move from side to side, but is otherwise responsive. My script seems correct. Any idea what the problem might be? Thanks!
Nope this is a platform patrol but I did do this in my recent game, you just shoot a raycast forward and check if it hits a collider that isn't the player...
@@MuddyWolf How did you do a 2nd RaycastHit2D collider without causing errors? I am able to define a 2nd RaycastHit2D with a new name however the moment I try to take action on the collider = false for the 2nd Raycast I get an error. Thanks
when i set it up and test it, on the #scene it shows the "enemy" getting to position one and doing the rotation perfect, on the "game" window it goes invisible like the sprite just hids, the object its still there working but i cant see it
Well I didn't find a video so I created it my self in the "EnemyPatrol" script you should paste this in at the buttem private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Bullet")) { Destroy(gameObject); } } If you would life you can just copy it all and it should work for you aswell :) using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyPatrol : MonoBehaviour { public float speed = 2f; public Rigidbody2D rb; public LayerMask groundLayers; public Transform groundCheck; bool isFacingRight = true; RaycastHit2D hit; private void Update() { hit = Physics2D.Raycast(groundCheck.position, -transform.up, 1f, groundLayers); } private void FixedUpdate() { if (hit.collider != false) { if (isFacingRight) { rb.velocity = new Vector2(speed, rb.velocity.y); } else { rb.velocity = new Vector2(-speed, rb.velocity.y); } } else { isFacingRight = !isFacingRight; transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f); } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Bullet")) { Destroy(gameObject); } } } I'm able to shoot my own enemies at least :)
i need help and i dont know what ive done wrong Error Message: Assets\Scripts\EnemyPatrol.cs(17,45): error CS1061: 'Transform' does not contain a definition for 'postition' and no accessible extension method 'postition' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?) My Code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyPatrol : MonoBehaviour { public float speed = 2f; public Rigidbody2D rb; public LayerMask groundLayers; public Transform groundCheck; bool isFacingRight = true; RaycastHit2D hit; private void Update() { hit = Physics2D.Raycast(groundCheck.postition, -transform.up, 1f, groundLayers); } private void FixedUpdate() { if (hit.collider != false) { if (isFacingRight) { rb.velocity = new Vector2(speed, rb.velocity.y); }else { rb.velocity = new Vector2(-speed, rb.velocity.y); } } else { isFacingRight = !isFacingRight; transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f); } } }
Well I can't find any misspells, or any wrong code, so I don't think anything is wrong and that particular script, it might be some of your other scripts, but I doubt it. It could also be you forget to give your enemy the enemy tag, or you missed something in the video, I would go through it and see if something was wrong. It could also be you that forgot to move the Rigidbody2D & groundCheck in the checker, you know the place where we can show the script what object we specifically mean. Hope that anything I said helped
you misspelled something so here is the fixed code (if you have not rewritten it by now) The Code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyPatrol : MonoBehaviour { public float speed = 2f; public Rigidbody2D rb; public LayerMask groundLayers; public Transform groundCheck; bool isFacingRight = true; RaycastHit2D hit; private void Update() { hit = Physics2D.Raycast(groundCheck.position, -transform.up, 1f, groundLayers); } private void FixedUpdate() { if (hit.collider != false) { if (isFacingRight) { rb.velocity = new Vector2(speed, rb.velocity.y); }else { rb.velocity = new Vector2(-speed, rb.velocity.y); } } else { isFacingRight = !isFacingRight; transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f); } } }
I used a sprite I had made myself instead of a circle, the script works but for some reason he looks really squashed, he’s like half his height but the same width. I checked my scale but those were both 1.5, does anyone know how to fix this
This is my entire script for enemy patrol. remember to put the "ground check" object into the ground check slot on unity in order to activate it. using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyPatrol : MonoBehaviour { public float speed = 2f; public Rigidbody2D rb; public LayerMask groundLayers; public Transform groundCheck; bool isFacingRight = true; RaycastHit2D hit; private void Update() { hit = Physics2D.Raycast(groundCheck.position, -transform.up, 1f, groundLayers); } private void FixedUpdate() { if (hit.collider != false) { if (isFacingRight) { rb.velocity = new Vector2(speed, rb.velocity.y); } else { rb.velocity = new Vector2(-speed, rb.velocity.y); } } else { isFacingRight = !isFacingRight; transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f); } } }
@@alexbingham6804 If I was you, I wouldnt even create these enemy patrol scripts for start. I would give the object a animation (with the „animation“ window) and then change the position in the timeline. Its way more easy and you can make objects go in every direction there
here is the script if anyone wants it: using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyPatrol : MonoBehaviour { public float speed = 2f; public Rigidbody2D rb; public LayerMask groundLayers; public Transform groundCheck; bool isFacingRight = true; RaycastHit2D hit; private void Update() { hit = Physics2D.Raycast(groundCheck.position, -transform.up, 1f, groundLayers); } private void FixedUpdate() { if (hit.collider != false) { if (isFacingRight) { rb.velocity = new Vector2(speed, rb.velocity.y); }else { rb.velocity = new Vector2(-speed, rb.velocity.y); } } else { isFacingRight = !isFacingRight; transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f); } } }
Hey when i leave my thing alone it says "not hitting ground", but when my guy stands on it, it has "hitting ground" maybe i screwed something else in the tilemap, as my guy has infinite jumps even tho i put the feat thing
Sorry about the quality! For some reason it exported at 720 instead of 1080p and It was too later before I noticed! Hopefully you can still learn something from the video!
I love this series! You explain things fast, clear, and correctly! Thank you so much. It was exactly what I was looking for. Like everything I needed to put in my game you explained to me. Thanks again!!!
Glad it was helpful!
Can you post the script you have?
Awesome, quick tip look at the camera while recording!
Thanks for the tip!
And what if I want to make my enemy stop at the corner, look around and then turn around?
i got confused on his tutorials so i did one on shooting on another guy did movement with brackeys and now im doing this XD
So quick question, is there any way i can change the sprite for the enemy as well as give it its own animation?
Hello, does anyone know how to make the ennemy speed not linear but smoother as he reaches the limit points ? Thanks !
hey where is you getting that ground option in ground layer script one
I had a syntax error and I don't know how to fix it
my line of code is the same as his so is it something in settings???
Hi there. My Patrolling Enemy won't move from side to side, but is otherwise responsive. My script seems correct. Any idea what the problem might be? Thanks!
It's me!
My enemy stops suddenly when i hit play. If I drag and drop him again, it moves longer. Anyone had this issue?
I have the same issue, but I don't know how to fix it.
gracias por todo ..eres un campeon...
#OnePixelAtATime
What if the enemy patrol runs into a wall? will the enemy rotate?
Nope this is a platform patrol but I did do this in my recent game, you just shoot a raycast forward and check if it hits a collider that isn't the player...
@@MuddyWolf How did you do a 2nd RaycastHit2D collider without causing errors? I am able to define a 2nd RaycastHit2D with a new name however the moment I try to take action on the collider = false for the 2nd Raycast I get an error.
Thanks
when i set it up and test it, on the #scene it shows the "enemy" getting to position one and doing the rotation perfect, on the "game" window it goes invisible like the sprite just hids, the object its still there working but i cant see it
Thats happening to me, did you manage to solve it
What program do you use to write the code? And version?
He's using VS Code. If you have it downloaded, it will auto open, whenever you double click on a script in Unity!
can you do a video so that when you shoot the enemy patrol with the bullet it dies please!!
Well I didn't find a video so I created it my self in the "EnemyPatrol" script you should paste this in at the buttem
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
Destroy(gameObject);
}
}
If you would life you can just copy it all and it should work for you aswell :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour
{
public float speed = 2f;
public Rigidbody2D rb;
public LayerMask groundLayers;
public Transform groundCheck;
bool isFacingRight = true;
RaycastHit2D hit;
private void Update()
{
hit = Physics2D.Raycast(groundCheck.position, -transform.up, 1f, groundLayers);
}
private void FixedUpdate()
{
if (hit.collider != false)
{
if (isFacingRight)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
}
else
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
}
else
{
isFacingRight = !isFacingRight;
transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
Destroy(gameObject);
}
}
}
I'm able to shoot my own enemies at least :)
@@dannill6314 thanks
i need help and i dont know what ive done wrong
Error Message: Assets\Scripts\EnemyPatrol.cs(17,45): error CS1061: 'Transform' does not contain a definition for 'postition' and no accessible extension method 'postition' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)
My Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour {
public float speed = 2f;
public Rigidbody2D rb;
public LayerMask groundLayers;
public Transform groundCheck;
bool isFacingRight = true;
RaycastHit2D hit;
private void Update() {
hit = Physics2D.Raycast(groundCheck.postition, -transform.up, 1f, groundLayers);
}
private void FixedUpdate() {
if (hit.collider != false) {
if (isFacingRight) {
rb.velocity = new Vector2(speed, rb.velocity.y);
}else {
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
} else {
isFacingRight = !isFacingRight;
transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f);
}
}
}
Well I can't find any misspells, or any wrong code, so I don't think anything is wrong and that particular script, it might be some of your other scripts, but I doubt it. It could also be you forget to give your enemy the enemy tag, or you missed something in the video, I would go through it and see if something was wrong. It could also be you that forgot to move the Rigidbody2D & groundCheck in the checker, you know the place where we can show the script what object we specifically mean.
Hope that anything I said helped
@@dannill6314 it was that script because i ended up fixing it myself about a hour later. but thanks!
@@Steppy hey, how did fix it?
You misspelled "position" in
hit = Physics2D.Raycast(groundCheck.postition, -transform.up, 1f, groundLayers);
you misspelled something so here is the fixed code (if you have not rewritten it by now)
The Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour {
public float speed = 2f;
public Rigidbody2D rb;
public LayerMask groundLayers;
public Transform groundCheck;
bool isFacingRight = true;
RaycastHit2D hit;
private void Update() {
hit = Physics2D.Raycast(groundCheck.position, -transform.up, 1f, groundLayers);
}
private void FixedUpdate() {
if (hit.collider != false) {
if (isFacingRight) {
rb.velocity = new Vector2(speed, rb.velocity.y);
}else {
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
} else {
isFacingRight = !isFacingRight;
transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f);
}
}
}
I used a sprite I had made myself instead of a circle, the script works but for some reason he looks really squashed, he’s like half his height but the same width. I checked my scale but those were both 1.5, does anyone know how to fix this
Nvm I figured it out it was 1.5 in the scale but only 1f in the code
For me, the enemy stops at the cliff but doesn't turn around
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour {
public float speed = 2f;
public Rigidbody2D rb;
public LayerMask groundLayers;
public Transform groundCheck;
bool isFacingRight = true;
RaycastHit2D hit;
private void Update() {
hit = Physics2D.Raycast(groundCheck.position, -transform.up, 1f, groundLayers);
}
private void FixedUpdate() {
if (hit.collider != false) {
if (isFacingRight) {
rb.velocity = new Vector2(speed, rb.velocity.y);
}else {
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
} else {
isFacingRight = !isFacingRight;
transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f);
}
}
}
THANKS!!!!!!
RIP the 1080p, for a person with bad eye sight this was hard to follow.
its not working for me
FixedJoint :D
Mine just falls right of the edge 😔
This is my entire script for enemy patrol.
remember to put the "ground check" object into the ground check slot on unity in order to activate it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour
{
public float speed = 2f;
public Rigidbody2D rb;
public LayerMask groundLayers;
public Transform groundCheck;
bool isFacingRight = true;
RaycastHit2D hit;
private void Update()
{
hit = Physics2D.Raycast(groundCheck.position, -transform.up, 1f, groundLayers);
}
private void FixedUpdate()
{
if (hit.collider != false)
{
if (isFacingRight)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
}
else
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
}
else
{
isFacingRight = !isFacingRight;
transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f);
}
}
}
I dont even have this Ground Layer
I don't know if you've been following the entire video series. We originally created the Ground layer for your platform.
@@OutCold9te6 Ah sorry I was dumb. It takes 2 secs to create one, but I didnt know. I was new in pogramming
@@simn.353 As someone that is also dumb... how do you create it...
@@alexbingham6804 If I was you, I wouldnt even create these enemy patrol scripts for start. I would give the object a animation (with the „animation“ window) and then change the position in the timeline. Its way more easy and you can make objects go in every direction there
@@simn.353 thanks I’ll check that out !
here is the script if anyone wants it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour {
public float speed = 2f;
public Rigidbody2D rb;
public LayerMask groundLayers;
public Transform groundCheck;
bool isFacingRight = true;
RaycastHit2D hit;
private void Update() {
hit = Physics2D.Raycast(groundCheck.position, -transform.up, 1f, groundLayers);
}
private void FixedUpdate() {
if (hit.collider != false) {
if (isFacingRight) {
rb.velocity = new Vector2(speed, rb.velocity.y);
}else {
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
} else {
isFacingRight = !isFacingRight;
transform.localScale = new Vector3(-transform.localScale.x, 1f, 1f);
}
}
}
Hey when i leave my thing alone it says "not hitting ground", but when my guy stands on it, it has "hitting ground" maybe i screwed something else in the tilemap, as my guy has infinite jumps even tho i put the feat thing