Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Shooting Both Directions in 2D

Discussion in 'Scripting' started by PirateMonkey, Jun 28, 2014.

  1. PirateMonkey

    PirateMonkey

    Joined:
    Jun 24, 2014
    Posts:
    28
    Hi,

    I am trying to get my 2D character to be able to shoot in both +x and -x directions, but am having some difficulties. With my current script, I can start by shooting in the direction the character faces, but then as soon as you change direction, the missiles change direction too.

    I have figured out this is because of the if statements in the MissileMove script (see attached). However, I have not been able to find a clean solution to this. I think I can get it to work by creating two missile prefabs and two missile shoot scripts (one for left and one for right) but this seems far too complicated.

    What is an easy solution to this? I would also love if you could show me with code!

    Thanks!!!
     

    Attached Files:

  2. crag

    crag

    Joined:
    Dec 13, 2013
    Posts:
    145
    So the missile is a child of the player? If so, you could reparent the missile to root level or even a missile container. Then the missile won't be affected by the character's localScale.x.
     
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Cache the direction the player is facing at the time the missile is fired, and use this value in your missile move script.
     
  4. PirateMonkey

    PirateMonkey

    Joined:
    Jun 24, 2014
    Posts:
    28
    I was thinking about that earlier...How do you cache an action at a specific movement?
     
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    In this case looking at your code, I would probably pass the value of SubControl.IsTurnedRight to the missile movement script when you fire the missile and store it in a variable, and use that variable's value in your missile movement logic instead of how you are currently doing it.
     
    PirateMonkey likes this.