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 Daniel311311 · Aug 25, 2014 at 05:35 AM · c#shootingaimingsidescroller

How can I make character arm rotation/mouse aim work correctly?

link textSo I have a dilemma. I'm following along with a 2D platformer tutorial to learn some basics and I have one issue, that is I need my arm to flip/ change directions with the player (which it currently does) so that the gun and arm I designed looks right aiming in the correct direction. The issue I run into is that when the arm flips it will no longer aim correctly. If anyone has ideas as to how I can fix this it would be much appreciated. alt text

2d issues.jpg (174.6 kB)
2dplayercharacter.txt (3.6 kB)
Comment
Add comment · Show 6
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 kacyesp · Aug 25, 2014 at 05:39 AM 0
Share

Should the player be able to shoot left if he is facing right?

avatar image Daniel311311 · Aug 25, 2014 at 03:02 PM 0
Share

Ok, here is the script I have in place to rotate the arm. It is attached to the arm itself, while the arm/gun are a child of the Player.

using UnityEngine; using System.Collections;

public class ArmRotation : $$anonymous$$onoBehaviour {

 public int rotationOffset = 90;
 PlatformerCharacter2D player;


 
 // Update is called once per frame
 void Update () {
             

 
     // subtracting position of player from mouse position
     Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;  
     difference.Normalize ();       //normalizing the vector. $$anonymous$$eaning all the sum of the vector will be equal to 1.

     float rotZ = $$anonymous$$athf.Atan2 (difference.y, difference.x) * $$anonymous$$athf.Rad2Deg;
     transform.rotation = Quaternion.Euler  (0f, 0f, rotZ + rotationOffset);
 }

}

avatar image Daniel311311 · Aug 25, 2014 at 03:09 PM 0
Share

Then I have this as my script for my player character.

link text

2dplayercharacter.txt (3.6 kB)
avatar image meat5000 ♦ · Aug 25, 2014 at 03:15 PM 0
Share

I'm not a guru on this by any means but I'm quite sure flipping the scale won't automatically flip everything else.

avatar image Daniel311311 · Aug 25, 2014 at 03:18 PM 0
Share

The arm does flip, but then the issue you see in the second pic occurs when I move the cursor to aim to the left. Prior to moving the cursor the arm faces the left along with the rest of the character. I don't know if it the problem is that I need to do something to the rotation of the arm or what.

Show more comments

1 Reply

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

Answer by robertbu · Aug 26, 2014 at 06:23 AM

Here is a bit of code. To use:

  • Setup your sprite so that the front of the sprite is facing right. Given your code, right now you have it facing up.

  • Attach this script

  • Disable whatever logic is currently flipping the the texture


    using UnityEngine; using System.Collections;

    public class Example : MonoBehaviour { private float x; private Vector3 ls;

       void Start() {
             x = transform.localScale.x;
             ls = transform.localScale;
         }
         
         void Update () {
             Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint (transform.position);  
     
             float rotZ = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
     
             if (dir.x >= 0) {
                 transform.rotation = Quaternion.Euler  (0f, 0f, rotZ);
     
                 ls.x = x;
                 transform.localScale = ls;
             }
             else {
                 transform.rotation = Quaternion.Euler  (0f, 0f, rotZ+180);
                 ls.x = -x;
                 transform.localScale = ls;
             }
         }
     }
    
    
    
    
    
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 Daniel311311 · Aug 26, 2014 at 06:47 AM 0
Share

Just try it and it works like a charm. Thanks, I knew it was something super simple but I've been working at it forever. $$anonymous$$ost other threads I see with a similar issue don't end up getting this solution so I've been going in circles. Thanks again.

avatar image SlaireStudios · Nov 28, 2014 at 03:39 AM 0
Share

Hey, so I been having the same problem, and i tried using your script but is says that it is not a valid script. Why is that?

avatar image robertbu · Nov 28, 2014 at 04:23 PM 0
Share

@SlaireStudios - You'll have to be specific about the error. Any chance you pasted this script into a Javascript file? It is C#.

avatar image wenson16 · Jan 17, 2016 at 05:08 PM 0
Share

@robertbu , I know this is quite old and your solution worked at some point. However, since we disabled the flipping function, the body wont flip to the left now. The arms rotate correctly though.

I tried adding your code to the body(which in my case is the parent of the arms) while removing the " transform.rotation = Quaternion.Euler (0f, 0f, rotZ);" and "transform.rotation = Quaternion.Euler (0f, 0f, rotZ+180);" part. The body flips left and right using the mouse pointer however the arms go back to its previous issue. Is there a proper solution for this?

avatar image theangrybagelbox · May 13, 2020 at 08:44 AM 0
Share

@wenson16 I was having similar issues in my game. I'm doing things a little differently though, since things are in 3d (but the gameplay is 2D). What I did was set the arm rotation to Quaternion.Euler(0f, 0f, rotZ) if the direction is to the right, and set it to Quaternion.Euler (180f, 0f, rotZ*-1) if the direction is to the left. I don't know if it'll work in this scenario, but I spend a crap ton of my time trying to figure it out so I thought i'd go ahead and share. Hope it helps.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Aiming with the mouse in a 2d shooter, and then shooting to the mouse 2 Answers

Bullets follow mouse after shooting in Sidescroller 1 Answer

Help me! I cant aim properly 2 Answers

Sidescroller Bullets Follow Mouse After Being Shot 2 Answers

[Closed] Enemy Shooting isn't working correctly 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