Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Frap1K · May 09, 2017 at 05:21 AM · playershooting2d platformer2d rotationfacing

Unity 2D On Player Flip change shooting direction?

Hello everyone I've been looking everywhere online to fix my problem but couldn't find what i was looking for, wondering if someone could help me out here, Thanks! Basically I have a player with normal controls left,right & jump. I have a hand that is completely separated from the player body. the hand has a pivot to the body to stay in place on the player. When the player flips the hand flips with the player to the direction player facing. I have followed Brackeys tutorial to create 360 hand rotation and shooting - https://www.youtube.com/watch?v=4ivFemmpYus∈dex=10&list=PLPV2KyIb3jR42oVBU6K2DIL6Y22Ry9J1c

When my player is flipped right with the hand the bullets go to the direction where the firePoint is pointing at, (360 degrees.) alt text

But my problem is when player is flipped left with the hand the bullets continue going to the same direction like the player is still flipped to the right. alt text

Player character script: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerCharacter : MonoBehaviour {

 //movement varibles

 Rigidbody2D rb;
 bool facingRight = true;


 [SerializeField] float maxSpeed = 10f;
 [SerializeField] float jumpForce = 400f;

 [Range(0, 1)]
 [SerializeField] float crouchSpeed = .36f;

 [SerializeField] bool airControl = false;
 [SerializeField] LayerMask whatIsGround;

 Transform groundCheck;
 float groundedRadius = .02f;
 bool grounded = false;
 Transform ceilingCheck;
 float ceilingRadius = .01f;
 Animator anim;

 Transform playerGraphics;                      //reference to the graphics so we can change direction.


 //reference to the graphics so we can change direction

 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }

 void Awake()
 {
     //setting up references.
     groundCheck = transform.Find("GroundCheck");
     ceilingCheck = transform.Find ("CeilingCheck");
     anim = GetComponent<Animator> ();
     playerGraphics = transform.FindChild ("Graphics");
     if (playerGraphics == null) {
         Debug.LogError ("Lets freak out! there is no 'Graphics' object as a child of a player");
     }
 }

 void FixedUpdate()
 {
     //The player is grounded if a circlecast to the groundcheck position hits anything disignated as ground
     grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);
     anim.SetBool ("Ground", grounded);

     //Set the vertical animation
     anim.SetFloat("vSpeed", rb.velocity.y);
 }
     

 public void Move(float move, bool crouch, bool jump)
 {
     //if crouching check if the character can stand up, keep them crouching
     if (!crouch && anim.GetBool ("Crouch")) {
         if (Physics2D.OverlapCircle (ceilingCheck.position, ceilingRadius, whatIsGround))
             crouch = true;
     }

     //Set whether or not the character is crouching in the animator
     anim.SetBool ("Crouch", crouch);

     //only control the player if grounded or airControl is turned on
     if (grounded || airControl) {
         //reduce the speed if crouching by the crouchSpeed multiplier
         move = (crouch ? move * crouchSpeed : move);

         //the speed animator parameter is set to the absolute value of the horizontal input.
         anim.SetFloat ("Speed", Mathf.Abs (move));

         //Move the character
         rb.velocity = new Vector2 (move * maxSpeed, rb.velocity.y);

         //If the input is moving the player right and the player is facing left...
         if (move > 0 && facingRight)
             //... flip the player.
             Flip ();
         //Otherwise if the input is moving the player left and the player is facing right...
         else if (move < 0 && !facingRight)
             //...flip the player.
             Flip ();
     } 

     // if the player should jump...
     if (grounded && jump) {
         //Add a vertical force to the player.
         anim.SetBool ("Ground", false);
         rb.AddForce (new Vector2 (0f, jumpForce));
     }
 }

 void Flip ()
 {
     //Switch the way the player is labelled as facing.
     facingRight = !facingRight;

     //Multiply the player's x local scale by -1.
     Vector3 theScale = playerGraphics.localScale;
     theScale.x *= -1;
     playerGraphics.localScale = theScale;
 } 

}

PlayerControlKeys: using UnityEngine;

namespace UnitySampleAssets2D {

 [RequireComponent(typeof (PlayerCharacter))]
 public class playerControlKeys : MonoBehaviour
 {
     private PlayerCharacter character;
     private bool jump;

     private void Awake()
     {
         character = GetComponent<PlayerCharacter>();
     }

     private void Update()
     {
         if(!jump)
             // Read the jump input in Update so button presses aren't missed.
         if (Input.GetButtonDown("Jump")) jump = true;
     }

     private void FixedUpdate()
     {
         // Read the inputs.
         bool crouch = Input.GetKey(KeyCode.LeftShift);
         float h = Input.GetAxis("Horizontal");
         // Pass all parameters to the character control script.
         character.Move(h, crouch, jump);
         jump = false;
     }
 }
     

}

ArmRotation: using UnityEngine; using System.Collections;

public class ArmRotation : MonoBehaviour {

 public int rotationOffset = 90;

 // Update is called once per frame
 void Update () {
     // subtracting the position of the player from the mouse position
     Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
     difference.Normalize ();        // normalizing the vector. Meaning that all the sum of the vector will be equal to 1

     float rotZ = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg;    // find the angle in degrees
     transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotationOffset);
 }

}

Weapon: using UnityEngine; using System.Collections;

public class Weapon : MonoBehaviour {

 public float fireRate = 0;
 public float Damage = 10;
 public LayerMask whatToHit;

 public Transform BulletTrailPrefab;
 public Transform MuzzleFlashPrefab;
 float timeToSpawnEffect = 0;
 public float effectSpawnRate = 10;

 float timeToFire = 0;
 Transform firePoint;

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

 // 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 () {
     Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
     Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
     RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
     if (Time.time >= timeToSpawnEffect) {
         Effect ();
         timeToSpawnEffect = Time.time + 1/effectSpawnRate;
     }
     Debug.DrawLine (firePointPosition, (mousePosition-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.");
     }
 }

 void Effect () {
     Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation);
     Transform clone = Instantiate (MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
     clone.parent = firePoint;
     float size = Random.Range (0.6f, 0.9f);
     clone.localScale = new Vector3 (size, size, size);
     Destroy (clone.gameObject, 0.02f);
 }

}

Any help would be really appreciated! Thank you for your time!

zgame2.png (96.4 kB)
zgame1.png (69.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kerandon · Jun 13, 2020 at 01:02 AM

Did you figure it out? I have the same problem

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Gun not firing in certain direction 1 Answer

Why do the player's health points go down so fast? 1 Answer

How to damage player when bullet collides with player. 0 Answers

Having an issue with one script on multiple GameObjects 0 Answers

How can i change Angry Bots player shooting direction from x, z axis to x, y? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges