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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Flipping shooting point

Discussion in '2D' started by marius234582, Nov 26, 2020.

  1. marius234582

    marius234582

    Joined:
    Nov 15, 2020
    Posts:
    6
    Hello,

    I am making 2D shooter platformer game and I have my player flipping to mouse direction and weapon following mouse cursor. However, the shootpoint will not flip too and I dont know how to flip anything withou sprite :D. Can anyone help me with this one?

    Thank you
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    How are you currently flipping your character?
     
  3. marius234582

    marius234582

    Joined:
    Nov 15, 2020
    Posts:
    6
    var delta = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

    if (delta.x >= 0 && !facingRight) { // mouse is on right side of player
    transform.localScale = new Vector3(1,1,1); // or activate look right some other way
    facingRight = true;
    } else if (delta.x < 0 && facingRight) { // mouse is on left side
    transform.localScale = new Vector3(-1,1,1); // activate looking left
    facingRight = false;
    }
    THIS IS IN MY PLAYERCONTROLLER SCRIPT

    AND THIS IS IN MY SHOOT SCRIPT

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class ShootScript : MonoBehaviour
    {

    public float offset;

    private GameObject player;
    private SpriteRenderer spriteren;
    public GameObject Bullet;
    public Transform ShootPoint;
    public Animator anim;

    public float BulletSpeed;
    public float FireRate;
    float readyForNextShoot;

    private void Start()
    {
    player = GameObject.FindGameObjectWithTag("Player");
    spriteren = GetComponent<SpriteRenderer>();
    }


    void Update()
    {
    Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);

    if(rotZ < 89 && rotZ > -89)
    {
    spriteren.flipY = false;
    spriteren.flipX = false;
    }
    else
    {
    spriteren.flipY = true;
    spriteren.flipX = true;
    }

    if (Input.GetButtonDown("Fire1"))
    {
    if(Time.time > readyForNextShoot)
    {
    readyForNextShoot = Time.time + 1 / FireRate;
    shoot();
    }
    }
    }

    void shoot()
    {
    GameObject BulletIns = Instantiate(Bullet, ShootPoint.position, transform.rotation);
    BulletIns.GetComponent<Rigidbody2D>().AddForce(BulletIns.transform.right * BulletSpeed);
    anim.SetTrigger("Shoot");
    }



    }
     
  4. marius234582

    marius234582

    Joined:
    Nov 15, 2020
    Posts:
    6
    upload_2020-11-26_23-22-4.png
    upload_2020-11-26_23-22-33.png
     
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Make sure when posting code to use the code tags so it is formatted correctly.

    Both scripts are essential doing the same thing. I would have it control the arm in the player script along with the flipping(so you are only calculating once) and have a gun or weapon script that controls the firing/attacking. One caveat is that when it's flipped left with -1 scale it will invert the rotation, here is a snippet from a game I'm working on.

    Code (CSharp):
    1. float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    2. if (Flipped)
    3.     angle += 180f;
    4.  
    5. armPivot.rotation = Quaternion.Euler(0f, 0f, angle);
    I am not flipping any spriterenderer's, setting the scale.x to -1 already does that for me. I have no offset, I have the arm and the sprites facing right by default.
     
  6. marius234582

    marius234582

    Joined:
    Nov 15, 2020
    Posts:
    6
    Your way doesnt flip the gun when it cross 90 or -90 angle and I still have to flip char. Do you have any advice how to solve this my little problem please? Thank you
     
  7. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    It might be how you have it setup.

    This is how I setup my player:
    - Player Body Sprite - Has the Player script that controls the arm pivot rotation
    - Arm Pivot(Shoulder) - Empty game object that is what get rotated
    - Arm Sprite - Only the arm sprite
    - Gun Attach Point - Empty game object allows swapping weapons
    - Gun - Has the Gun script that controls firing and reloading.​

    When I set the scale.x to -1 on the Player root (the body sprite) it flips every child including positions. I only worry about the rotation input for the arm when the player is flipped as it also flips how it rotates. I also have the body, head, arms, legs and feet as separate sprites.

    Not sure how your player is setup, I have this kind of setup for a few different projects and have had no issues.