Getting my character to shoot

So i’m re-creating the old Gun Fight from the commadore and atari…

I’m reached to the point where i need my player to be able to shoot from his shotgun

Here’s a picture

The red bandana guy is the player and i want him to only be able to shoot 1 way and that’s straight.You have to press Space to shoot not mouse click.

Can anyone help with that? how do i do that

Update() {
     if(Input.GetKey(KeyCode.Space)) {
     //DO SHOOT STUFF
     }
}

also you can use

Input.GetKeyDown(KeyCode.Space)

Input.GetKeyUp(KeyCode.Space)

depending on how you want to build up your shooting system

Thanks mate :slight_smile: That was pretty easy. What exactly do i do under the //Do shoot stuff?

How fast the bullets goes
what *bullet it needs to recon etc?

This is my script now

if you want to shoot wihle holding space, you need a timer so that the shot/bullet won’t be spawned every Update

Pseudocode:

if(space pressed)
   if(timer is over)
      Shoot()
      Reset Timer

your timer var can be called shootfrequency so you will now later for what you have it.

Shoot() will be a method that is spawning a bullet. than you can move it with AddForce or the bullet prefab you are spawning has his own script on it moving it on the axis you want.

I have a bullet i just made and imported it in.
Script now looks like this

I’m sorry for being so newbie at this scripting. It’s rather hard for one who don’t work with it everyday.

So i can do
if (timer is over)
Shoot (“Bullet”) Bullet is the name of my bullet prefab i made.

And with timer is over i’m gonna add

+3 for 3 seconds delay between each shot?

if you want to know how to set up a timer you can google “Unity Timer” also youtube has many tutorial videos.

Also you will need

for spawning bullets

Also you should read about Collision and Collision Detection.

Thanks i managed to get it to work now. Fully functional of page 10 of google :smile: