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 D3m0nE · Aug 07, 2013 at 02:46 PM · 2drotationrotate

Unity 2D Rotate Problem!

alt text

Hello Everyone I'm Trying To Make 2D Shooting Game

but i just need a little help in rotating

i Use This Code

     private var mousePos : Vector2;
     private var screenPos : Vector3;
      
     function Update () {
     mousePos = Input.mousePosition;
     screenPos = Camera.main.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, transform.position.z - Camera.main.transform.position.z));
      
     transform.rotation.eulerAngles.z = Mathf.Atan2((screenPos.y - transform.position.y), (screenPos.x - transform.position.x))*Mathf.Rad2Deg;
     }

To Rotate The Gun.

but the problem that i need To Rotate The Gun 180 Degree Only in Right Side

if i did 181 Degree or More. i want to rotate The Whole Body to other side.

edit2.png (20.5 kB)
Comment
Add comment · Show 2
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 robertbu · Aug 07, 2013 at 03:04 PM 0
Share

I'm trying to figure out what you are asking here. $$anonymous$$y best guess is this:

alt text

That is when the cursor is place as on the left panel, the gun has this orientation, and when the cursor is as on the right panel, the gun is flipped.

If this is what you want, it might be easier to have two textures and flip the textures for each angle rather than write rotation code to do the filp. Plus you have the added complication in a flip that planes are one sided.

2dflipangle.png (23.1 kB)
avatar image D3m0nE · Aug 07, 2013 at 03:12 PM 0
Share

not exactly

when cursor is on the right panel then moved to left panel

i want the whole character to rotate to the left panel not only the weapon

sorry for my bad English

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 07, 2013 at 05:39 PM

The easy way to do this is to use a empty game object with the visible object as a child. You would then detect the left/right and do whatever flip you wanted to the child.

 dir = screenPos - transform.position;
 if (Vector3.Dot(dir, Vector3.right) > 0) {
     child.localRotation = Quaternion.Euler(0,0,0);
 }
 else {
     child.localRotation = Quaternion.Euler(0,180,0);
 }

The rotations here are just placeholders. You will need to figure out what rotation you want in the inspector and then fill in the correct values. I would be more efficient to precalculate the rotations in Start() and then just assign the Quaternion in Update().

 Start() {
     qLeft = Quaternion.Euler(0,180,0);
     qRight = Quaternion.Identity;
 }

 


Comment
Add comment · Show 5 · 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 D3m0nE · Aug 07, 2013 at 07:13 PM 0
Share

Sorry, but i Didnt Get it ..

here is the CodE :

     private var mousePos : Vector2;
     private var screenPos : Vector3;
      
     public var child : Transform;
      
     function Update () {
     
     
    mousePos = Input.mousePosition;
    screenPos = Camera.main.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, transform.position.z - Camera.main.transform.position.z)); 
    transform.rotation.eulerAngles.z = $$anonymous$$athf.Atan2((screenPos.y - transform.position.y), (screenPos.x - transform.position.x))*$$anonymous$$athf.Rad2Deg;
   
   
   
    
      var  dir = screenPos - transform.position;
     if (Vector3.Dot(dir, Vector3.right) > 0) {
   
      child.localRotation = Quaternion.Euler(0,0,0);
     }
     else {
     child.localRotation = Quaternion.Euler(0,180,0);
     }
    
     
       
           }
     



and this Script is attached to the Weapon Transform[Inside The Player] what is the child then?

anyway this is what happend when i added your code

alt text

cccccxxx.jpg (23.2 kB)
avatar image robertbu · Aug 07, 2013 at 07:34 PM 0
Share

As I mentioned in the answer, the values inside the Euler() were just placeholders. Play with other values until it works the way you want. With the gun pointed left, change the rotation in the inspector until you get what you want. Then use that value in the Euler() call in the code above. If something is going to work then it will be something like (180,0,0) or (0,0,180), or (90,180,0).

Note I suspect given the 2D and 3D nature of your problem, that this form of rotation might not give you what you want...that there will be no Euler() values that display the gun and the box correctly. This is especially true given that planes are one sided. That is why I suggested changing the texture rather than complex rotations to start with. If your problem can be solved by rotating, then some value in the Euler() call will work.

avatar image D3m0nE · Aug 07, 2013 at 08:51 PM 0
Share

Umm, its seems i need another solution

avatar image robertbu · Aug 07, 2013 at 09:31 PM 0
Share

When you tossed in the box, I was confused about how you constructed your game. I don't know the rotational relationship between the two, nor how you defined your pivot. Just guessing, I think all you need to do is flip the texture as the cursor crosses the boundary. The can flip the texture by changing the tiling/offset. You can use $$anonymous$$aterial.SetTextureScale(). The default scale is (1,1). Change either value to -1 to filp. You can use the same code above that calculates the Dot product to select left and right.

avatar image D3m0nE · Aug 08, 2013 at 01:18 AM 0
Share

okey, forgot about the Box can u give me code to flip the Weapon Horizontal?

i tried SetTextureScale , didnt work well for me

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

14 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

Related Questions

[Help] Object flips around when using transform.Rotate 3 Answers

Rotating a 2D GameObject on Z regardless of the distance. 2 Answers

problem with rotation 2D 1 Answer

How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers

Function to rotate my Player smoothly taking the y and use it to have angle 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