Reply To: UNIT SPACE SHOOTER….The shot spawn stays at fixed location and doesn’t move with ship.

Forum Archive UNIT SPACE SHOOTER….The shot spawn stays at fixed location and doesn’t move with ship. Reply To: UNIT SPACE SHOOTER….The shot spawn stays at fixed location and doesn’t move with ship.

#32112
Jonathan Gonzalez
Participant

    I’ll explain this a bit further. The “shotSpawn” is used as a position of where the “shot” should originate. Currently in your script you are using it as a GameObject which is fine, but not entirely necessary. In your script you have “shotSpawn.transform.position”, which is essentially saying “Use the transform of this game object, then use the position of that transform”. At the top of your script you have:

    public GameObject shotSpawn;

    I would change it to 

    public Transform shotSpawn;

    Since you’re just using this to get position information, you can use it as a transform directly and just write out “shotSpawn.position” instead. You don’t need to do this, it just simplifies things a bit. 

    The Instantiation lines are those where you instantiate the object and then move that instantiated object to a new location. Technically in your script it’s just one line, but the moving of the object can also be added into one line. This is what I’m, referring to:

    shotFired = Instantiate(shot) as GameObject;
     shotFired.transform.position = shotSpawn.transform.position;

    Here you are creating the object in the game (instantiating), then you are setting it’s position to the position of shotSpawn. This can all be done at once when it is created as such:

    Instantiate(shot, shotSpawn.position, Quaternion.identity) as GameObject;

    Instantiate is a method that can contain multiple parameters. In your script you’re just using the very basic version it by saying you want to instantiate an object, but typically when you use instantiate you’re creating something and placing it somewhere specific in the game world. So this is what you’d use:

    Instantiate(YourPrefab, Position, Rotation);

    First you instantiate something, usually a prefab, then the other two allow you to determine at what position, and what rotation that instantiated object should be. There are more parts to that method, but that’s mostly what you’ll use. 

    I know it can be tough to understand this stuff at times, I always like to know the what and why of these mechanics. If you’d like me to explain further let me know. 

    Also if you haven’t already I’d recommend this course: https://cgcookie.com/course/c-bootcamp-for-unity I’ll be remaking it in the near future but it’s a good starting point for learning C#.