Search Unity

The script relation between a player and bullet

Discussion in '2D' started by ventos, Sep 24, 2016.

  1. ventos

    ventos

    Joined:
    Sep 24, 2016
    Posts:
    4
    Hi, i downloaded unity yesterday and is totally new to it (and c#)

    I managed to get a player to move around. by using input.Getkey in the players update function.

    I want to spawn a bullet from the player, so i made a bullet object and dragged it into a prefeb, i hope this is correct.

    I'm trying to figure out how to spawn the prefab bullet. Do i make another script for the bullet prefab and then call from my players update function, or is everything kept within the players script

    I don't necessary need code examples, just need to understand how its put together (though simple code just to spawn the object would be nice as well)

    Thanks in advance
    Ventos
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    its up to you but the more object-oriented way probably is to put a script on the bullet itself which moves the bullet, and maybe detects collisions (if you're using colliders). then you make it a prefab. then you have a script on the player which 'instantiates' a bullet at a given coordinate - the bullet then sort of 'propels itself' with its own script.
     
  3. Scarletor

    Scarletor

    Joined:
    Sep 24, 2016
    Posts:
    6
    Hi there im' new to unity too,

    Is there someone tell me what is object-oriented by some example? i really don't understand what is it when read wiki.

    About the main of topic, i think if you code some action for bullet, you should put it in the bullet, or else when you want to change your code for bullet, you dont know how to find lol.
     
  4. ventos

    ventos

    Joined:
    Sep 24, 2016
    Posts:
    4
    thank you.

    @scaletor, i cant really answer you here. im new as well :)
     
  5. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    Object Oriented programming is where the program waits until an object is activated to do something. This might be a mouseclick on a button, or in the case of a game when gameobjects do things.

    There are a tonne of other ways of a how programming langues are built. Most programmers would be familiar with top-down, where the program starts at line 1 and keeps going until the code runs out. Object orient programming uses Top-down programming, but when the code runs out the program simply goes back to waiting for another event to kick off more code.