ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก
Turret Fire - Build a 2D Tower Defence Game in Unity #5
ฝัง
- เผยแพร่เมื่อ 27 เม.ย. 2023
- Learn how to make a 2D tower defence game using Unity. In this video, we apply a Shooting mechanic to our Turrets and then destroy our Enemies in our 2D Tower Defense game built in Unity.
Next Video: • Building Towers - Buil...
Previous Video: • Turrets - Build a 2D T...
Playlist: • 2D Tower Defence Tutor...
The source code can be found on Patreon (link below)
// SUPPORT THE CHANNEL
/ muddywolf
// JOIN THE COMMUNITY DISCORD!
/ discord
// PLAY MY DAMN GAME ALREADY?
play.google.co...
// WEB DEV CHANNEL
/ tylerpotts
#UnityTutorial #2D #TowerDefense
Are you enjoying this series so far? I know I am!
yea
Yeah I love this series. Iv'e been looing for a tutorial like this for weeks😊
yes!!😃
No. What are you doing??? you jump between 50 parts of DIFFERENT code instead of working on one part. Why are you assigning variables throughout, you should assign them all at the start. You ignore precision because it's 'fun'????????? What on earth are you doing, you should NOT be let in proximity to a computer. On top of this, you have ZERO understanding of editing, copying as soon as you do something is NOT a good idea. You are speaking at a volume of 3 DECIBELS. Talk louder and learn how to make a tutorial before claiming to be SO advanced you can make a n11 PART tutorial where each part gets NOTHING done.
I really enjoy your series, though you are really fast! And here ´n there i really don´t understand how things work or how to "learn" that properly to remember it and transfer to another project. I have a lot of coding experience, but those gaming scripts are sth different :D. I´m just following along your nice tutorials, let´s see in the end.
Very cool series
Thank you!
Thank you so much for this video. You really saved my project
Happy to help!
this has been great! Is there a DestroyBullet command when they miss? I don't see that...but they are adding up ;0
I keep getting an error that says Assests\Code\Scripts\Bullet.cs(30,9): error CS0103: The name ‘other’ does not exist in the current context
Me too bruh, have you managed to fix this? If yes say how plz
check the input parameter name. It might be "collision" instead of "other"
thankyou respected brother
i didnt understood the SetTarget Function can anyone teach me thanks
I chose a different Bullet design, which has an elongated shape. How can I rotate my bullet in the right direction when it is fired right at the beginning? For this you only have to retrieve the rotation value from the turret and transfer it to the bullet, but how exactly? Otherwise it hits the Enemy with the elongated side instead of the front, pointed side.
Might be a bit late, but the answer is actually rather simple. All you have to do on the bullet script is set the rotation of the bullet to the direction vector: transform.up = direction;
Its funny how fucked up my code is even though i am following your vids. Thanks Regardless
Great Videos very informative but I have one small issue when the bullets collide with the enemies they do not take any damage. The hitpoints dont go down and neither the bullet or the enemy disappear. Any help would be appreciated
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour
{
[Header("References")]
[SerializeField] private Rigidbody2D rb;
[Header("Attributes")]
[SerializeField] private float bulletSpeed = 5f;
[SerializeField] private int bulletDamage = 1;
private Transform target;
public void SetTarget(Transform _target)
{
target = _target;
}
private void FixedUpdate()
{
if(!target) return;
Vector2 direction = (target.position - transform.position).normalized;
rb.velocity = direction * bulletSpeed;
}
private void OnCollisionEnter2D(Collision2D other)
{
other.gameObject.GetComponent().TakeDamage(bulletDamage);
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour
{
[Header("Attributes")]
[SerializeField] private int hitPoints = 2;
public void TakeDamage(int dmg)
{
hitPoints -= dmg;
if (hitPoints
@@jameshylands4175 Not sure if you're still having this issue but could potentially be due to collision boxes not being assigned correctly or that the objects are different heights in the z axis. This is what caused it for me
I somehow have a trouble, even though i created bullet layer and assigned it to bullet and in project setting physics 2D i have ticked off everything besides Enemy, the bullet still hits the wall. Im trying to figure it out, since 40 minutes or so but i have no clue why
Like it doesnt destroy itself but it stops
same problem
I do the step in the min 11:28 but the bullet stuck in the floor somebody now why?
I had the same problem because I had written...
_target = target
... instead of...
target = _target
@@morganp7238 Send the complete script i renember the error was easy to fix
My firingposition is fixed in place where i place it. I does not move accordingly to the turret. Fix?
you put the firingpostion under the rotationpoint in the hierarchy
For some reason my bullet is meeting the transform of the enemy, but the game object isn't destroying upon colliding with the enemy...
Ah, for some reason my OnCollisionEnter2D script will only work if I add a Boxcollider2D component to my prefab, then exclude the default layer... even though I changed all of the settings :o at least it's working... for now haha
3:15 JB Bloc Boy on the console
I keep getting this NullReferenceExxception object reference not set to an instance of an object Bullet. OncollisionEnter2D
I am having a problem where when the bullet is fired, it just stays in the spot it was fired from, any idea why?
I had this issue, it was resolved by going through the instructions at 11:10 on the video. While I thought I had changed the Layer Collision Matrix it hadn't saved for me
@@paulsiddall6074 i fixed it but now the bullet is not colliding with the enemy either 🙃
Can someone help I couldn’t attach reagedbody to bullet
If I comment the following out the game seems to work fine,
private void OnCollisionEnter2D(Collision2D collision)
{
//collision.gameObject.GetComponent().TakeDamage(bulletDamage);
Destroy(gameObject);
}
however, when I run the game with the code I get the following:
NullReferenceException: Object reference not set to an instance of an object
BulletScript.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Code/Scripts/BulletScript.cs:32)
It happens as soon as the bullet spawns.
It happened with Other instead of collision as well.
Any ideas?
I am having this problem as well. It seems like Unity is having trouble identifying the target as the gameObject? The fix that Visual Studio is recommending is that I add a void variable "v" but it seems like the recommendation will only create more problems. I am not sure how to fix this. I would appreciate an update.
Curious? What does your Health script look like?
@@robertmcdougall1144
public class Health : MonoBehaviour
{
[Header("Attributes")]
[SerializeField] private int hitPoints = 2;
public void TakeDamage(int dmg)
{
hitPoints -= dmg;
if(hitPoints
@@robertmcdougall1144 public class Health : MonoBehaviour
{
[Header("Attributes")]
[SerializeField] private int hitPoints = 2;
public void TakeDamage(int dmg)
{
hitPoints -= dmg;
if(hitPoints
@@robertmcdougall1144 public class Health : MonoBehaviour
{
[Header("Attributes")]
[SerializeField] private int hitPoints = 2;
public void TakeDamage(int dmg)
{
hitPoints -= dmg;
if(hitPoints