Making Your First Game: Unity 3D Zombie Shooter Platformer – shooting code
September 6, 2017 1 min read
Hello everyone and welcome to another episode of 3D Prototyping in Unity. In this series we are creating a 3D (2.5D) Side Scrolling Zombie Shooter.
30 thoughts on “Making Your First Game: Unity 3D Zombie Shooter Platformer – shooting code”
help when i shoot in doesnt go quikly to the walk any help
Still loving these videos. I'm not following them exactly but they often come in handy as I port my Sonic engine from Game Maker to Unity.
Hello there and Seriously thanks for these tutorials, I have a question: is it possible to aim with our Mouse position and shoot bullets in other directions (in a platformer 2.5D Game like the one your doing)?
Hi I'm going over your tutorial and chose not to use raycast hit I'm shooting the model via /// this is a seperate script for the bullet only called bulletmover void Start () { GetComponent<Rigidbody>().velocity = transform.forward * speed; }
/// this is a seperate script for the bullet only called bulletmover
It's working fine when character is facing towards the right but when I turn the character left the player bullet still goes towards the right direction. Any way I can fix this I went in and looked at the rotation of the bullet spawner and bullet everything is good any suggestions?
I wound up adding this and it seemed to fix the turning in one direction to another as well as shooting. But now its jittery. Here is the code I tested while commenting out Flip() method.
transform.Rotate( 0f, 90f, 0f); //turns and shoots the right way but jittery
Hi, I have this problem ( https://ibb.co/nAkk3v ), you know why my bullets go in that direction?
my shoot bullets don't quit faceing camera no matter what i do
i have no errors and linrenderer is not working and have all what you have now
how do you make the bullets spread up and down using your method of quaternion.euler (rot)? I can't seem to create gun spread without breaking the system of shooting left and right. btw I have watched all the videos in this series, and it has helped me a lot of understanding C# and how 2.5D shooter works. Cheers mate!!
is there any program that i can use to make 2d characters?
if(myPlayer.GetFacing() == -1f) {
I get an error on this line in unity. It says "Object reference not set to an instance of an object".
On the player controller, I have the GetFacing as followed:
public float GetFacing(){ if(facingRight) return 1; else return -1;
Hello Casanis!!! I had some problems with my Line Renderer here. Now its fixed, but, looks like it have some problems updating parameter in game. I mean, i changed the parameter start and end width a few times and nothing happened, the gun keep drawing the same size os light (bigger then the size of the soldier head) After that, a created a new material with a new color to make sure that im changing the correct component. i changed the material in the component and it doesnt changed in game again… after trying, doing and redoing everything, it started to work correctly (and confirmed that i was changing the correct parameter), but i still dont know what happened… seems like this line renderer get little bugged in my Unity… have some tips???
Thanks and keep the good work. Cheers from Brazil
Hello, I'm having trouble getting the weapon to fire correctly. The particle effects are coming from the gun but not the bullet, although in the inspector i see the bullets spawning. Another note, when i walk to the edge of the platform and fire the weapon i can see the bullets appearing in the z axis, but only when i walk to the edge of the platform. I've copied the code line for line and checked everything thoroughly, the code is identical to yours but no luck, I've been stuck on this video for the last 3 days trying to figure this out. I did notice that when i added the line renderer component, i had to uncheck the use world space box in order for the line to appear by the soldier even after resetting the component, and i noticed you didn't have to do that. But even after leaving it checked the bullets still doesn't not appear properly. Any idea what the issue could be?
Thank you very much, offer good channel
halo casanis,
if i want make the character shoot bullet 90 degrees, not just staright forward, so we can shoot the enemy from up and down, what should we add to firebullet script ?
Hello Mr.Casanis !, first of all thank you for this, and secondly, I know this is a beginner tutorial, but in the firebullet script you are getting playerController Component every frame because you have written it in the update and this is very costly, when you should get it just once on the Start or Awake. And I think that beginners they should know a little about instruction's cost, and thank you
I am a dum dum… Took me a while to realize that if I am using a freaking sphere and if I want the model to flip I should use the x cordinate not the z cordinate…
I realize that just now.
Shouldnt we use object pooling instead of destroying/instantiating the object himself it could save a lot of cpu performance and its also a great practice to learn
hello me is throwing error on this line of code : Instantiate ( projectile , transform.position , Quaternion.Equals (rot ) ) ; are these errors:
Assets / Screenplay / Firebullet.cs ( 33,11 ) : error CS1503 : Argument '# 3' can not convert ` bool ' expression to write ` UnityEngine.Quaternion ' ———————————————– Assets / Script / Firebullet.cs ( 33,11 ) : error CS1502 : The best overloaded method match for ` UnityEngine.Object.Instantiate ( UnityEngine.Object , UnityEngine.Vector3 , UnityEngine.Quaternion ) 'has some invalid arguments ————————————————– Assets / Script / Firebullet.cs ( 33,66 ) : error CS0120 : An object reference is required to access non – static member ` UnityEngine.Quaternion.Equals (object ) ' —————————————— Assets / Script / Firebullet.cs ( 33,73 ) : error CS0841 : A local variable ` rot 'can not be used before it is Declared
HELP
Hi Guy! Thank for this great Tutorial. I download the Asset but i don´t have a "bullet" Objekt in my Asset? How can i get the "bullet" Object for this Tutorial? Thank!
Casanis I did everything you did and ı downloaded your models. but my soldiers skin not like yours my soldier is white. and when i press the key "D or A" the character not walking, soldier like crawling. when i press the shift key then soldier walking. whats wrong ?
Casanis, how can I make that the soldier does not move while in WALK mode? I have a shoot animation but he does not move the legs so I want to freeze the movement in shoot mode. Thank you!
hi +CasanisPlays
im on the shootbullet part but i keep getting an error, i copied everything exactly the way you shown but i still get an error about an unexpected symbol '}'. please take a look and get back AsAp. thanks!
using UnityEngine; using System.Collections;
public class shootBullet : MonoBehaviour {
public float range = 10f; public float damage = 5f;
Ray shootRay; RaycastHit shootHit; int shootableMask; LineRenderer gunLine;
// Use this for initialization void Awake () { shootableMask = LayerMask.GetMask("Shootable"); gunLine = GetComponent<LineRenderer>();
// Update is called once per frame void Update () {
} }
Hi, very nice content and materials! Would be very cool to integrate video number in the video link description to follow along though. Awesome tutorials so far!
why if i fire the gun the effects are coming out on the ground not to the gunMuzzle??
Hi Casanis. Please a little help, I don't know what I did wrong. I checked my code twice and it is the same as yours. When I hit the fire button two bullets appear, one from the gun tip and one from the head of my player.
Hey, at the end after all the coding my bullets are in a continuous stream even tho im not pressing my mouse button Ive got all the scripts right, but for somereason its not working
Great tutorial series! very simple and easy to follow! keep the good work all kids need tutorials like yours! cheers!
Hey, just found your channel and i think it's awesome! you remind me a lot of Andrew Price. Keep going ; D
help when i shoot in doesnt go quikly to the walk any help
Still loving these videos. I'm not following them exactly but they often come in handy as I port my Sonic engine from Game Maker to Unity.
Hello there and Seriously thanks for these tutorials, I have a question:
is it possible to aim with our Mouse position and shoot bullets in other directions (in a platformer 2.5D Game like the one your doing)?
Hi I'm going over your tutorial and chose not to use raycast hit I'm shooting the model via
/// this is a seperate script for the bullet only called bulletmover
void Start ()
{
GetComponent<Rigidbody>().velocity = transform.forward * speed;
}
/// this is a seperate script for the bullet only called bulletmover
It's working fine when character is facing towards the right but when I turn the character left the player bullet still goes towards the right direction. Any way I can fix this I went in and looked at the rotation of the bullet spawner and bullet everything is good any suggestions?
void ShootingDesktop()
{
if(Input.GetButtonDown("Fire1"))
{
Instantiate (bullet, bulletSpawnPoint.position, bulletSpawnPoint.rotation);
print("We are firing");
}
}
I wound up adding this and it seemed to fix the turning in one direction to another as well as shooting. But now its
jittery. Here is the code I tested while commenting out Flip() method.
transform.Rotate( 0f, 90f, 0f); //turns and shoots the right way but jittery
Hi, I have this problem ( https://ibb.co/nAkk3v ), you know why my bullets go in that direction?
my shoot bullets don't quit faceing camera no matter what i do
i have no errors and linrenderer is not working and have all what you have now
how do you make the bullets spread up and down using your method of quaternion.euler (rot)? I can't seem to create gun spread without breaking the system of shooting left and right. btw I have watched all the videos in this series, and it has helped me a lot of understanding C# and how 2.5D shooter works. Cheers mate!!
is there any program that i can use to make 2d characters?
if(myPlayer.GetFacing() == -1f) {
I get an error on this line in unity. It says "Object reference not set to an instance of an object".
On the player controller, I have the GetFacing as followed:
public float GetFacing(){
if(facingRight) return 1;
else return -1;
Hello Casanis!!!
I had some problems with my Line Renderer here. Now its fixed, but, looks like it have some problems updating parameter in game.
I mean, i changed the parameter start and end width a few times and nothing happened, the gun keep drawing the same size os light (bigger then the size of the soldier head)
After that, a created a new material with a new color to make sure that im changing the correct component. i changed the material in the component and it doesnt changed in game again…
after trying, doing and redoing everything, it started to work correctly (and confirmed that i was changing the correct parameter), but i still dont know what happened… seems like this line renderer get little bugged in my Unity…
have some tips???
Thanks and keep the good work.
Cheers from Brazil
Hello, I'm having trouble getting the weapon to fire correctly. The particle effects are coming from the gun but not the bullet, although in the inspector i see the bullets spawning. Another note, when i walk to the edge of the platform and fire the weapon i can see the bullets appearing in the z axis, but only when i walk to the edge of the platform. I've copied the code line for line and checked everything thoroughly, the code is identical to yours but no luck, I've been stuck on this video for the last 3 days trying to figure this out. I did notice that when i added the line renderer component, i had to uncheck the use world space box in order for the line to appear by the soldier even after resetting the component, and i noticed you didn't have to do that. But even after leaving it checked the bullets still doesn't not appear properly. Any idea what the issue could be?
Thank you very much, offer good channel
halo casanis,
if i want make the character shoot bullet 90 degrees, not just staright forward, so we can shoot the enemy from up and down, what should we add to firebullet script ?
Hello Mr.Casanis !, first of all thank you for this, and secondly, I know this is a beginner tutorial, but in the firebullet script you are getting playerController Component every frame because you have written it in the update and this is very costly, when you should get it just once on the Start or Awake. And I think that beginners they should know a little about instruction's cost, and thank you
I am a dum dum…
Took me a while to realize that if I am using a freaking sphere and if I want the model to flip I should use the x cordinate not the z cordinate…
I realize that just now.
Shouldnt we use object pooling instead of destroying/instantiating the object himself it could save a lot of cpu performance and its also a great practice to learn
hello me is throwing error on this line of code :
Instantiate ( projectile , transform.position , Quaternion.Equals (rot ) ) ;
are these errors:
Assets / Screenplay / Firebullet.cs ( 33,11 ) : error CS1503 : Argument '# 3' can not convert ` bool ' expression to write ` UnityEngine.Quaternion '
———————————————–
Assets / Script / Firebullet.cs ( 33,11 ) : error CS1502 : The best overloaded method match for ` UnityEngine.Object.Instantiate ( UnityEngine.Object , UnityEngine.Vector3 , UnityEngine.Quaternion ) 'has some invalid arguments
————————————————–
Assets / Script / Firebullet.cs ( 33,66 ) : error CS0120 : An object reference is required to access non – static member ` UnityEngine.Quaternion.Equals (object ) '
——————————————
Assets / Script / Firebullet.cs ( 33,73 ) : error CS0841 : A local variable ` rot 'can not be used before it is Declared
HELP
Hi Guy! Thank for this great Tutorial. I download the Asset but i don´t have a "bullet" Objekt in my Asset? How can i get the "bullet" Object for this Tutorial? Thank!
Casanis I did everything you did and ı downloaded your models. but my soldiers skin not like yours my soldier is white. and when i press the key "D or A" the character not walking, soldier like crawling. when i press the shift key then soldier walking. whats wrong ?
Casanis, how can I make that the soldier does not move while in WALK mode? I have a shoot animation but he does not move the legs so I want to freeze the movement in shoot mode. Thank you!
hi +CasanisPlays
im on the shootbullet part but i keep getting an error, i copied everything exactly the way you shown but i still get an error about an unexpected symbol '}'. please take a look and get back AsAp. thanks!
using UnityEngine;
using System.Collections;
public class shootBullet : MonoBehaviour {
public float range = 10f;
public float damage = 5f;
Ray shootRay;
RaycastHit shootHit;
int shootableMask;
LineRenderer gunLine;
// Use this for initialization
void Awake () {
shootableMask = LayerMask.GetMask("Shootable");
gunLine = GetComponent<LineRenderer>();
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
gunLine.SetPosition(0,transform.position);
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask)) {
gunLine.SetPosition(1,shootHit.point);
}else gunLine.SetPosition (1, shootRay.origin+shootRay.direction*range)
}
// Update is called once per frame
void Update () {
}
}
Hi, very nice content and materials! Would be very cool to integrate video number in the video link description to follow along though. Awesome tutorials so far!
why if i fire the gun the effects are coming out on the ground not to the gunMuzzle??
Hi Casanis. Please a little help, I don't know what I did wrong. I checked my code twice and it is the same as yours. When I hit the fire button two bullets appear, one from the gun tip and one from the head of my player.
Hey, at the end after all the coding my bullets are in a continuous stream even tho im not pressing my mouse button
Ive got all the scripts right, but for somereason its not working
Great tutorial series! very simple and easy to follow! keep the good work all kids need tutorials like yours! cheers!
Hey, just found your channel and i think it's awesome! you remind me a lot of Andrew Price. Keep going ; D
nice!
please subtitle on.
thanks!
very good best 2D channel