Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Doom-Holme · Jun 11, 2020 at 08:02 PM · scripting problem2d sprites2d animation2d rotationflipping

Problem with 2D gun rotation script (Would really apreaciate help)

I'm gonna provide you with a Gif of the Problem. The problem seems to be that the Pivot of the gun is attached to the Player, and the flip in the CharacterController script seems to double flip the gun. But I honestly have no clue how to fix it. I'll provide you with the code aswell.

 public float speed = 5f;
 public Rigidbody2D rb;
 bool facingRight = true;
 Vector2 move;
 public Animator animator;

 void Update()
 {
     move.x = Input.GetAxisRaw("Horizontal");
     move.y = Input.GetAxisRaw("Vertical");

     animator.SetFloat("Speed", move.sqrMagnitude);

     // using mousePosition and player's transform (on orthographic camera view)
     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;
     }
 }

 private void FixedUpdate()
 {
     rb.MovePosition(rb.position + move.normalized * speed * Time.fixedDeltaTime);
 }

Thats the character Controller

 public GameObject myPlayer;

 private void FixedUpdate()
 {

     Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

     difference.Normalize();

     float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

     transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);

         if (rotationZ< -90 || rotationZ> 90)
     {
         if(myPlayer.transform.eulerAngles.y == 0)
         {
             transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
 
         } else if (myPlayer.transform.eulerAngles.y == 180) { 
             transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
         }
     }
 }

And thats the gun pivot script, the gun sprite being attached to the pivot point which itself is attached to the Player object. I would be really thankful for some help. Thanks in advance.

kirby-holding-a-gun.gif (156.6 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 ShadyProductions · Jun 11, 2020 at 08:07 PM

Why don't you just do the same with the gun like u did the player instead of messing with localrotation, just invert the localScale

  transform.localScale = new Vector3(-1, 1, 1); // right
  transform.localScale = new Vector3(1, 1, 1); // left
Comment
Add comment · Show 2 · 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 Doom-Holme · Jun 11, 2020 at 08:36 PM 0
Share

Hello, thank for the quick answer. Where would I put that code in?

avatar image Doom-Holme · Jun 11, 2020 at 08:42 PM 0
Share

I just tried to replace the localRotation with localScale but it didn't seem to fix the problem

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

232 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 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 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 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 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 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 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 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 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

How to keep Diagonal Movement Direction in 2D Isometric game? 0 Answers

help with freezing 2D sprite rotation 2 Answers

How to get a sprite to move in the direction it is facing . 1 Answer

How to flip hitboxes if my 2D Sprite is Flipped in AnimationsEditor ? [Unity2D] 1 Answer

2D Animation plays for a split second unless i spam the input button,2D Animation plays for a split second unless i spam the input button 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