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
1
Question by VenturaEstudio · Aug 03, 2013 at 01:05 AM · 2daim

2D shooter plataform aim,tutorial please?

Hi, i´m kind newbie in script.I following a mario clone tutorial,but i want a shooter,and you know, with the gun following the mouse. i searched alot of things about this,and i found but , the only one that worked ( i just copy and paste):

  #pragma strict
  
 var mouse_pos : Vector3;
 var target : Transform; 
 var object_pos : Vector3;
 var angle : float;
  
 function Update ()
 {
     mouse_pos = Input.mousePosition;
     mouse_pos.z = -20;
     object_pos = Camera.main.WorldToScreenPoint(target.position);
     mouse_pos.x = mouse_pos.x - object_pos.x;
     mouse_pos.y = mouse_pos.y - object_pos.y;
     angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
 }
 
 Aim = new Vector3((target.transform.position.x - transform.position.x), target.transform.position.y - transform.position.y, 0);
     transform.rotation = Quaternion.FromToRotation (Vector3(0, 5, 0), Aim);


,didnt work very well(the object(player ) that should rotate following the mouse,follows, but it turn 180 in Y too,and as is a plane(2d)the texture face the other side ,so just becomes invisible,but i know that still follows the x,y of the mouse).

As i kind dont understand this code ,i wish someone could please help,with a explanation for noobs about the 2d aim system,or just solve the problem that i got with the current code its gonna make me wonderfully grateful as well.

Comment
Add comment · Show 1
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 RavenOfCode · Sep 26, 2015 at 11:58 PM 0
Share

Thanks for sharing this, really helped alot :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by boni · Aug 03, 2013 at 06:23 AM

As you'll know, Unity uses 3D coordinates, however the screen (the thing the camera displays) is 2D, as well as your mouse-coordinates.

The first 2 Lines in the Update

 mouse_pos = Input.mousePosition;
 mouse_pos.z = -20;

obtain the position of the mouse cursor on the screen and actually moves the position 20 "in front" of the screen. Next, the targeted objects position is obtained and transformed into screen-space:

 object_pos = Camera.main.WorldToScreenPoint(target.position);

What this does, is that it takes the position of the object target in the game engine (the usual x/y/z coordinate) and transforms it to the coordinate system of the screen. So if you see an object ingame, and it's, let's say, 100 pixels from the bottom and from the left on your screen, this operation actually gives you that: a Vector3 that says it's (100,100,Z).

Now here comes the weird part with the Z-coordinate. As we can read in the WorldToScreenPoint-Documentation, "the z position is in world units from the camera". I assume the guy who wrote this script had his camera 20 units away from his objects, that's why he sets the mouse_pos.z to -20 above. Personally I'd just set all the Z positions to 0, to get 2D-Coordinates no matter where the camera is.

 mouse_pos.x = mouse_pos.x - object_pos.x;
 mouse_pos.y = mouse_pos.y - object_pos.y;
 angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;

This part is simple, straight forward math. ;) He takes the X and Y distance of the mouse and the object on the screen, and calculates the angle. Just imagine a right angle triangle with the catheti beeing the x and y distance. In case you don't know trigonometry well, just know that tan(angle) = delta_y/delta_x, and therefore the angle = atan(y/x), which is exactly what MathF.Atan2(y,x) does. :)

 transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));

Quaternions represent a rotation. Quaternion.Euler returns the rotation around the X/Y/Z axis defined by a Vector3. If the Vector3 is (0,0,45), that means it'll be rotated by 45° around the z axis. Therefore this line rotates the object to look towards the target-object, since that's what "angle" represents.

I'm not exactly sure what the Aim-Line at the bottom is supposed to be, since it does nothing except transform.rotation = Quaternion.FromToRotation(Vector3.up*5, Vector3.zero). When is that thing called?

What I'm presonally missing in this script, is a section where he checks wether the mouse cursor is to the left or to the right of the object, since you'll have to mirror the gun if the mouse cursor is to the left, or it'll stand on its head. :)

Comment
Add comment · Show 3 · 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 VenturaEstudio · Aug 03, 2013 at 04:14 PM 0
Share

Thanks ,that´s cleared my $$anonymous$$d.

First ,about the 2 last lines,that shouldn't be there(i just paste alot of codes that i found,just to figure it out,i forgot to erase that ),my fault.

And i cant put this code to work right,just one time it worked and than stopped,maybe is my scene configurtion or i´m placing the script in the wrong place,but the thing is that the target turn around,and turn its other face to the camera when start the "game",i even try to solve rotating everything as well ,but in this way the target follows the mouse inversely.
alt text

imagemaer.png (109.3 kB)
avatar image boni · Aug 03, 2013 at 07:45 PM 0
Share

Ah, if I understand correctly the Y-axis is mirrored/inversed for the angle, right? I suppose this is because the Screen-Space has its root (0/0) at the bottom left, and positive numbers go upwards, but you're using the top-left with positive numbers going down as the root?

If it's the case, a simple Screen.height-y should fix it. Another idea, if the angle is just behaving really weird, is trying to switch the parameters for atan. Atan2(x,y) ins$$anonymous$$d of y,x.

If one of these doesn't fix it.. then I fear i don't fully understand the problem. :)

avatar image RavenOfCode · Sep 26, 2015 at 11:59 PM 0
Share

Thanks for sharing this it really helped alot :) Thumps Up :)

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

2D Animation does not start 1 Answer

Problem with gun rotation after flipping character (2D) 1 Answer

Shooting around defense position without contact 1 Answer

SideScrolling Mouse Pointer Aim - Help 0 Answers

Aiming a shot in Unity 2d? 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