Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

2D Side scroller shooting question

Discussion in '2D' started by Shaquita, Nov 7, 2015.

  1. Shaquita

    Shaquita

    Joined:
    Jan 28, 2014
    Posts:
    37


    My game is gonna be a classic shooter side scrolling platformer but i dont want the player just shooting left and right. I want it to shoot up as well.

    The way my player shoots is from an empty GameObject (its selected in gif to show) in the gun that shoots the array and prefab(the bullet) (shown below)


    So since my character flips the local scale when Input Horizontal is down instead of rotatiing the player the FirePoint (The game objects name in whcih my player shoots from) does not rotate with my player even though its a child of the player.
    So therefore my FirePoint's rotation is always set to its default which is (0,0,0) andd always shoots to the right (shown above)

    I created a fix to this though to rotate the transform 'firepoint' (0, 180, 0) when player flips the local scale so the firepoint is always consistent with the player, atleast when moving left and right however.



    Now to the question on how to shoot up when Input Vertical (W) is down.
    So far ive tried rotating it (0,0,90) which faces up BUT it turns 90 degrees everytime i hold it down so it shoots all around my player, is there a way in unity just to set it to a specific roation without it adding 90 degrees of rotation everytime you have Input Vertical down? right now im using transform.Rotate(x,y,z)

    either that or adding a new game object (firePointUp) thats set above the player facing up so whenever the player shoots while input vertical it shoots through firePointUp.

    Help needed
     
  2. Prototypetheta

    Prototypetheta

    Joined:
    May 7, 2015
    Posts:
    122
    Try using transform.EulerAngles instead of transform.Rotate to rotate the fire point. That way you can specify exactly what you want the angle to change to. You'll need a function to change it back afterwards though.
    Code (CSharp):
    1. Vector3 oldRotation;
    2.  
    3. if(Input.GetKeyDown (KeyCode.UpArrow)){
    4. oldRotation = transform.eulerAngles;
    5.  
    6. transform.eulerAngles = new Vector3 (0,0,90);
    7. }
    8.  
    9. if(Input.GetKeyUp (KeyCode.UpArrow)){
    10. transform.eulerAngles = oldRotation;
    11. }
     
    Last edited: Nov 7, 2015
  3. Shaquita

    Shaquita

    Joined:
    Jan 28, 2014
    Posts:
    37
    Ill try that but wont be able to work on it till 10 tonight (Central america time)
    How about setting a bool to state true or false when looking up so when false all i gotta do is reset rotation
     
  4. Prototypetheta

    Prototypetheta

    Joined:
    May 7, 2015
    Posts:
    122
    I've edited my post to show a quick way of returning to the original rotation, whenever you hit the vertical input you can store the original rotation in a vector3 before you actually rotate it, then return to that when you lift off the input key.

    However using a bool to reset it would also do the job just fine. Might be better to do it with a bool as you could then access that function without having to have any player input. I don't know why you would do that but it seems more versatile.
     
unityunity