Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Domo23000 · Jan 10, 2015 at 04:25 AM · 2dscaleangleclamp

2D GunPivot Bug

In my game i use joystick axis X to move around and joystick axis 3 and 4 to rotate the gun around. The 3rd axis also flips the player. The clamp just prevents it from going too high for each player class. Everything works fine until i flip the player. Then the gun is either at the top of the clamp when 4th axis is greater than 0, bottom when it's less than 0, or in the middle when it's at 0. Here's the code :

 using UnityEngine;
 using System.Collections;
 
 public class GunMove : MonoBehaviour 
 {
     public int player = 0;
     string xAxis = "X P1";
     string yAxis = "Y P1";
     public float clamp;
     public bool isFacingRight=true;

     void Update () 
     {
         float x = Input.GetAxis(xAxis);
         float y = Input.GetAxis(yAxis);
 
         float angle = Mathf.Atan2(y,x) * Mathf.Rad2Deg;
         angle = Mathf.Clamp(angle, -clamp, clamp);
 
         transform.localEulerAngles = new Vector3(0, 0, angle);
 
         if(x > 0 && !isFacingRight)
         {
             Flip();
         }
 
         if(x < 0 && isFacingRight)
         {
             Flip();
         }
     }
 
     void Flip()
     {
         Vector2 parentScale = transform.parent.localScale;
         parentScale.x *= -1;
         transform.parent.localScale = parentScale;
 
         isFacingRight = !isFacingRight;
     }

Here's a video of the game bug

Thanks for any help

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
1
Best Answer

Answer by Bentley · Jan 13, 2015 at 01:28 AM

Negative scaling to flip sprites can cause weird issues. Try:

 parent.transform.localEulerAngles = new Vector3(0, 180);

Instead of:

 parentScale.x *= -1;

Hope this helps, Bentley

Comment
Add comment · Show 1 · 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
avatar image Domo23000 · Jan 18, 2015 at 07:14 PM 0
Share

Thanks a lot man this helped

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

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scaling my background Sprite to fill screen? (2d) 2 Answers

Moving camera by z axis in 2D 1 Answer

doubt with pixels per unit 1 Answer

2D C# top down shooter, adjusting the projectiles angle 1 Answer

Issues calculating angle in 2D 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