2D Platformer shooting


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…

using UnityEngine;
using System.Collections;

public class Shooting : MonoBehaviour {

    public GameObject shot;
    public float Damage = 10;
    public float fireRate = 0;

    float timeToFire = 0;
    Transform firePoint;

    // Use this for initialization
    void Awake () {
        firePoint = transform.FindChild("FirePoint");
        if (firePoint == null)
        {
            Debug.LogError("No FirePoint located");
        }

    }

// Update is called once per frame
void Update () {
        if (fireRate == 0)
        {
            if (Input.GetButtonDown("Fire1"))
            {
                Shoot();
            }
        }
        else
        {
            if(Input.GetButton ("Fire1") && Time.time > timeToFire)
            {
                timeToFire = Time.time + 1 / fireRate;
                Shoot();
            }
        }
   
    }

    void Shoot()
    {
        Instantiate(shot, firePoint.position, firePoint.rotation);
        Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
        RaycastHit2D hit = Physics2D.Raycast (firePointPosition, )
    }
}

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.

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.

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.

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?

thank for replying, i do need another perspective :slight_smile:

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.

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)

Transform endPoint;

Calling it in the this function (Script made by brackeys, modified to fit classic platformers)

 void Shoot()
    {
        Vector2 endPosition = new Vector2(endPoint.position.x, endPoint.position.y);
        Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
        RaycastHit2D hit = Physics2D.Raycast(firePointPosition, endPosition - firePointPosition, 100, whatToHit);
        Debug.DrawLine(firePointPosition, (endPosition-firePointPosition)*100, Color.cyan);
        if(hit.collider != null)
        {
            Debug.DrawLine(firePointPosition, hit.point, Color.red);
            Debug.Log("We hit" + hit.collider.name + " and did" + Damage + " damage");
        }
    }

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!

2443120–167593–Weapons.cs (3.12 KB)
2443120–167594–Weapons.cs (3.11 KB)