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 titovelosa · Jan 17, 2019 at 03:39 PM · 2d gamerotation axisflipflippingchild-to-gameobject

Unity - problem with arms rotation and flip, body flip and shotting - 2d shooter

I separated the main_body sprite from the arm sprite and made a script for body flip and another for arm rotation and flip, but now this happens:

alt text

If i make arm sprite child of main_body sprite i get this:

alt text

Here is my arm_rotation code:

 public class ArmRotation : MonoBehaviour
 {
     SpriteRenderer spriteRend;
     public Transform Firepoint;
 
 
     void Awake()
     {
         spriteRend = GetComponent<SpriteRenderer>();
     }
 
     void Update()
     {
         AimArmAtMouse();
     }
 
     void AimArmAtMouse()
     {
         Vector2 mousePosition = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);   //get the mouse position as a Vector2
         Vector2 armToMouse = mousePosition - (Vector2)transform.position; //get the vector from the arm to the mouse
         float rotationZ = Vector2.SignedAngle(transform.right, armToMouse);  //calculates the angle
         transform.Rotate(0f, 0f, rotationZ);  //rotates the transform by that angle
         FlipArm(Vector2.SignedAngle(transform.right, Vector2.right)); //calls the FlipArm method and passes in the angle of the arm relative to the world
     }
 
     void FlipArm(float rotation)
     {
         if (rotation < -90f || rotation > 90f)
         {
             spriteRend.flipY = true;
             FlipFirePoint(true);
 
         }
         else
         {
             spriteRend.flipY = false;
             FlipFirePoint(false);
  
         }
     }
     void FlipFirePoint(bool flip)
     {
         var pos = Firepoint.localPosition;
         pos.y = Mathf.Abs(pos.y) * (flip ? -1 : 1);
         Firepoint.localPosition = pos;
     }
 
 }

and my body rotation code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BodyRotation : MonoBehaviour
 {
 
 
     void Update()
     {
         Flip();
     }
 
     void Flip() //Flips player based on mouse's position relative to it's center.
     {
         Vector3 theScale = transform.localScale;
         Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
         float WorldXPos = Camera.main.ScreenToWorldPoint(pos).x; //Converts screen point to point in world
 
         if (WorldXPos > gameObject.transform.position.x)
         {
             theScale.x = 1;
             transform.localScale = theScale;
         }
 
         else
         {
             theScale.x = -1;
             transform.localScale = theScale;
         }
 
     }
 }

Any sugestion on how i can fix my problem? Thank you!

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 sh_code · Jan 17, 2019 at 03:51 PM

fixing the first case is easier - all you need to do is adjust your player's and arm's sprites' origin point so that they fit properly.

so that's what I recommend doing. start by adjusting the origin of the player so that when it flips around, the line which is his body, stays on the same place.

one way to achieve this would be to draw the body sprite in such a way that the body line is exactly in the center of the image, and then in unity, putting the origin in the horizontal center of the image as well. that might be enough for a fix.

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 titovelosa · Jan 17, 2019 at 08:31 PM 0
Share

It worked, @sh_code. Problem solved. Thanks again!

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

114 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

Related Questions

Flip 2D object on X axis because of a moving target. 1 Answer

Problem with counting front flips 1 Answer

Lock character's LocalScale during Idle animation 1 Answer

2D Sprite constantly flipping 0 Answers

,Why Character Is not Flipping ? There is some problem i am not able to see 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