Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

2D Platformer shooting

Discussion in '2D' started by ianmcelvain, Nov 4, 2015.

  1. ianmcelvain

    ianmcelvain

    Joined:
    Jan 28, 2014
    Posts:
    37



    How do i fix the position of the bullet sprite when character flips.
    Im using this as a reference but in code he uses his mouses position for the direction and not just vertical/horizontal.



    Heres the script im using, its not finished because i wouldnt know what to call to flip it.
    I have FirePoint gameObject at the tip of the assault rifle, i thought by default it would flip since its a child under character..
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shooting : MonoBehaviour {
    5.  
    6.     public GameObject shot;
    7.     public float Damage = 10;
    8.     public float fireRate = 0;
    9.  
    10.     float timeToFire = 0;
    11.     Transform firePoint;
    12.  
    13.     // Use this for initialization
    14.     void Awake () {
    15.         firePoint = transform.FindChild("FirePoint");
    16.         if (firePoint == null)
    17.         {
    18.             Debug.LogError("No FirePoint located");
    19.         }
    20.  
    21.     }
    22.  
    23. // Update is called once per frame
    24. void Update () {
    25.         if (fireRate == 0)
    26.         {
    27.             if (Input.GetButtonDown("Fire1"))
    28.             {
    29.                 Shoot();
    30.             }
    31.         }
    32.         else
    33.         {
    34.             if(Input.GetButton ("Fire1") && Time.time > timeToFire)
    35.             {
    36.                 timeToFire = Time.time + 1 / fireRate;
    37.                 Shoot();
    38.             }
    39.         }
    40.    
    41.     }
    42.  
    43.     void Shoot()
    44.     {
    45.         Instantiate(shot, firePoint.position, firePoint.rotation);
    46.         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
    47.         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, )
    48.     }
    49. }
    50.  
    51.  
     
  2. Prototypetheta

    Prototypetheta

    Joined:
    May 7, 2015
    Posts:
    122
    The way I'm doing it, is I have two fireTransform (same as your FirePoint) game objects that I use to spawn my bullet object. One faces left, one faces right (flipped 180 degrees on z axis)

    If you can detect which way your sprite is facing, you can choose which point to fire out of. I assume you're just flipping it's scale on the X-axis, so you can set the script to change the firing point based on what the x-scale of the player sprite is.
     
  3. ianmcelvain

    ianmcelvain

    Joined:
    Jan 28, 2014
    Posts:
    37
    yes you can use another transform var and call on it when player is facing the negative side of the local scale plane but i really do want to be efficient here and i can see bugs with that in the future.
     
  4. Prototypetheta

    Prototypetheta

    Joined:
    May 7, 2015
    Posts:
    122
    It's either that or stick a script on your FirePoint to move and rotate it to the correct place.

    EDIT: Or, just have one FirePoint, with no bells attached, and when you turn your sprite around, use the negative X of your firepoint and whack 180 degrees on the rotation. Should have the same effect. Two seperate fireTransforms is working fine for me so far but I have to replace that code with something a little more elaborate later on anyway.
     
  5. ianmcelvain

    ianmcelvain

    Joined:
    Jan 28, 2014
    Posts:
    37

    i only need the prefab to point to the direction of the gun. Im sure its an easy fix, is there any other tuts out there for it that i can cross reference?
     
  6. ianmcelvain

    ianmcelvain

    Joined:
    Jan 28, 2014
    Posts:
    37
    thank for replying, i do need another perspective :)
     
  7. Prototypetheta

    Prototypetheta

    Joined:
    May 7, 2015
    Posts:
    122
    Ah, idea. Instead of flipping localScale to turn the prefab around, turning it through 180 on the Y axis should have the same effect and in theory might just affect your FirePoint.
     
  8. ianmcelvain

    ianmcelvain

    Joined:
    Jan 28, 2014
    Posts:
    37
    Fixed it.

    While using an object as a substitute for the mousePosition that is just like FirePoint game object (just positioned alittle to the right) i was able to make it shoot straight in front of the gun no matter which way its facing.

    New variable (copy of firePoint just positioned to the right)
    Code (CSharp):
    1. Transform endPoint;
    Calling it in the this function (Script made by brackeys, modified to fit classic platformers)
    Code (CSharp):
    1.  void Shoot()
    2.     {
    3.         Vector2 endPosition = new Vector2(endPoint.position.x, endPoint.position.y);
    4.         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
    5.         RaycastHit2D hit = Physics2D.Raycast(firePointPosition, endPosition - firePointPosition, 100, whatToHit);
    6.         Debug.DrawLine(firePointPosition, (endPosition-firePointPosition)*100, Color.cyan);
    7.         if(hit.collider != null)
    8.         {
    9.             Debug.DrawLine(firePointPosition, hit.point, Color.red);
    10.             Debug.Log("We hit" + hit.collider.name + " and did" + Damage + " damage");
    11.         }
    12.     }
     
  9. mKiene

    mKiene

    Joined:
    Dec 25, 2015
    Posts:
    1
    Hi guys,

    My line won't draw to the end point. It acts as if there is another fixed end point below my player (when i jump its above my player) even though there isn't. here is my code:

    Thanks!
     

    Attached Files: