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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Kantus · Feb 18, 2015 at 08:59 PM · c#rotationweapon

Swapping the Position of an element on certain conditions

Hi all,

STRUCTURE: Player -> Arm -> (Various Weapon Classes)

I have a Player, which has a Child object called Arm. Arm rotates to follow the mouse cursor, but I wanted to avoid the weird situation where a player could "point" their weapon directly behind their character, thus sticking through the player's body. To do this, I prevented the Arm from pointing such that the X position was less than 0. This worked OK, but now I want to "flip" the situation when the player is facing left (so that Arm cannot be higher than 0). This is simple enough, but the problem comes in here:

I need to actually flip the arm's position in space, such that it is facing left rather than right. I also want to flip it such that the Weapon sprite (which is a child of Arm) is mirrored.

The problem is that, whenever I actually do so, the weapon's position is erratic and not what would be expected, and that if you wiggle right and left enough, sometimes the weapon will "break", the Z rotation will bizarrely become something like 270 (even though no code ever sets it to anything other than 0), and the weapon will detach itself from the Player and float a substantial distance beneath his torso. This is rather disconcerting.

The code is:

     void Flip ()
     {
         // Switch the way the player is labelled as facing.
         //also need to check which direction we're facing before flipping. Then, if facing right, move Arm's Transform
 
         Transform arm = transform.FindChild("Arm");
         float armXPos = arm.transform.position.x;
         float armYPos = arm.transform.position.y;
         float armYRotLeftToRight = 0;
         float armYRotRightToLeft = 180;
         //If we're facing right when the flip is ordered
         if (facingRight) {
             arm.transform.position = new Vector3((armXPos),(0), 0);
             arm.transform.rotation = Quaternion.Euler(0, armYRotRightToLeft, 0);         
         }
         else {
             arm.transform.position = new Vector3((armXPos * -1), 0, 0);
             arm.transform.rotation = Quaternion.Euler(0, armYRotLeftToRight, 0);       
             }
         facingRight = !facingRight;
         // Multiply the player's x local scale by -1.
         Vector3 theScale = playerGraphics.localScale;
         theScale.x *= -1;
         playerGraphics.localScale = theScale;
 
     }



Does anyone have any ideas as to how I might flip the weapon properly with the user?

Thanks.

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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Flip over an object (smooth transition) 3 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

LookAt + Raycasting makes weapon rotate in a weird way 0 Answers

Bullet flies away for no apparent reason. 1 Answer


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