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 Rain478293 · Jul 03, 2021 at 05:25 PM · rotationlocalscaleunderstand

I need help understanding piece of code

Hello, like title says, i need some help understanding some code. I'm making basic 2D game and i needed a script to move player's hand in the direction of the mouse position. I copied some code from the internet and it worked well until i decided that i want my player to look at the direction which he is moving. (right or left ). The way my script works is i have a pivot to which hand is attached and hand rotates around it. Here is the first piece of code : public GameObject Player;

 public Vector3 diffrerence;
 private void FixedUpdate()
 {

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

     diffrerence.Normalize();

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


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

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

 }


Everything is fine until i created this piece of code to the player's movement script:

     if (horizontal > 0)
     {
         
         transform.localScale = new Vector3(scale, transform.localScale.y, transform.localScale.y);
        
     }
     else if (horizontal < 0)
     {

         transform.localScale = new Vector3(-scale, transform.localScale.y, transform.localScale.y);

     }


When local scale is negative, player changes his direction but hand's rotation is completely broken. It's like inverted ? Here is the quick showcase of how it looks : alt text

So basically i know something is wrong with the rotation part of the script but i don't know what so i would appreciate if someone would explain how it works or would give me solution to the problem.

ezgifcom-gif-maker-1.gif (368.4 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Rain478293 · Jul 04, 2021 at 02:12 PM

Hi @Nistroy Thank you for your answer. I decided not to use SpriteRenderer because it doesn't change collider position but i used your advice to detach hand from the rest of the body. I made simple script that allows hand to have same transform as the player, with the offset available. I also checked if player is looking left or right by getting information about Horizontal Axis. This allowed me to change the offset to match the position i want to have hand on my player. Here is code for my solution :

 private void Update()
     {
         transform.position = new Vector3(Player.transform.position.x + xOffset, Player.transform.position.y + yOffset, transform.position.z);
 
         if(Player.GetComponent<MovementScript>().horizontal > 0)
         {
             xOffset = -0.07f;
         }
         else if (Player.GetComponent<MovementScript>().horizontal < 0)
             {
             xOffset = 0.07f;
             }
         
 
     }

and this is the piece from other script related to this:

 horizontal = Input.GetAxisRaw("Horizontal");


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 Nistroy · Jul 06, 2021 at 03:48 PM 0
Share

I'm glad you found the expected result!

avatar image
0

Answer by Nistroy · Jul 04, 2021 at 09:17 AM

Hi @Rain478293 ! Is the hand a child object of the character's base artwork? If so, then drag it so that the rotation of the character does not affect the rotation of the hand. If you ever want a child object of the base design to remain, then you need to flip the SpriteRenderer like this:

      if (horizontal > 0)
      {
          spriteRenderer.flipX = false;
      }
      else if (horizontal < 0)
      {
          spriteRenderer.flipX = true;
      }
 
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

161 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

Related Questions

Add a Vector3 to the rotation of an obj and have it behave with logic! 0 Answers

How do I flip my enemy... One scrpit works, however it shrinks my enemy aswell... The other ones i've commented out tend to make the enemy flip multiple times 1 Answer

Instantiated object not rotating around the center 0 Answers

Flip over an object (smooth transition) 3 Answers

Affine transformations to Object 3 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