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 Kensei · Jan 13, 2014 at 10:56 PM · 2d rotation2d character2d controller

2D Top Down Character Control

Hi guys, I've recently started using Unity, and I'm very new to C#, so I decided to make a test game of sorts in order to get used to the more common methods before I start making games. For that purpose I set the framework to 2D defaults and am just using a cube resting on a plane for the player. I'm having trouble getting the cube to rotate in order to face the mouse pointer. Here's my controller script

     void Update ()
     {
         // Get smoothed input
 
         float MoveHorizontal = Input.GetAxis ("Horizontal") * moveSpeed;
         float MoveVertical = Input.GetAxis ("Vertical") * moveSpeed;
 
         // Apply movement
 
         transform.Translate (Vector2.right * MoveHorizontal * Time.deltaTime);
         transform.Translate (Vector2.up * MoveVertical * Time.deltaTime);
 
         Vector2 mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         transform.LookAt (mousePos);

However, after doing some research I've realized that LookAt cannot be limited to using only x and y. I tried transform.rotate, but that doesn't take Input.mousePosition as an argument.

I did some more digging and found several similar questions here, but people are making some more complicated things like having some type of action attached to the mouse. I've tried some of the example codes, and funny enough the one that I understood the least, work the most. Even so it didn't really work properly, and I couldn't figure out what the problem was. Also I don't really want to use something that I don't understand. Just as an example, here's the code I found around here:

 //        Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
 //        Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
 //        lookPos = lookPos - transform.position;
 //        float angle = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
 //        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Like I said it kind of worked, but the controls were reversed and even inverting them from the Input manager didn't help. I also tried changing a few signs but that didn't work either. I've also tried Quaternion.Slerp but that only takes from and to parameters.

Sorry for asking such a simple question but I really done quite a bit of searching, and either I missed something or I really suck at programming and can't take a hint from another answer. I only want the cube to rotate on around it's z axis in order to face the mouse. Nothing more, I've found people needing to cast rays and to clamp movement to 90 degrees only, I don't need anything like that.

Also, while on the controller subject, is it better to use transform.Translate or use rigidbody.velocity for movement? Would that work on a cube or even a character, I mean it wouldn't just push the model around making it tumble like a misshaped piece of wood? Or rigidbody.AddForce (that only worked on a sphere though)? Also, since its a 2D game, is it pointless to have a cube? Can I just go with a quad? And if I can how would the 2D collider work if I place the quad at -1z and enemy shoots a bullet at -2z? If it wouldn't register does that mean that I'm limited to 1 type of collision per axis unit, or is this where c# layers come in play?

I'm very sorry if I'm overloading with stupid simple questions, but everywhere I look has answers that either I can't understand or include a load of other things that I don't have the questions for atm. Any explanation on any of these matters is highly appreciated. Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by l-forner · Jan 13, 2014 at 11:47 PM

Hi.

First thing first, welcome to Unity ^^

Have you started your project by checking the box 2D project? It simplifies many things when working only in 2D. And yes it's useless to have a cube in pure 2D, but many 2D-games nowadays use 3D models (super smash bros as a well known example), so that's your choice, also depending if the player would see a difference.

If you checked the 2D box and use a sprite (jpg, png, ...) instead of a cube, I think you wouldn't have trouble to rotate it only on z. But just because I'm not sure: have you tried to freeze rotation of the cube on x and y axes (in the rigidbody component)?

About coding the controller, I just know it's a bad idea to use translate or rotate as they don't handle collisions properly.

About the z axis, the simplest stay to launch some tests ;P

Good luck!

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 Kensei · Jan 15, 2014 at 04:59 AM 0
Share

Yes its pure 2D and i did tick the defaults on startup. You would say that LookAt uses only 2 axis when attached to a sprite?

avatar image
0

Answer by robertbu · Jan 13, 2014 at 11:40 PM

The code outline in the comments will work if you left the setup as default. Default means the camera has a rotation (0,0,0) and therefore facing positive 'z'. If you are using a perspective rather than an orthographic camera, you need to be careful about the use of '10' in the first line. It will be the distance in front of the plane of the camera to the plane of your 2D action. The 10 is not necessary for an orthographic camera. Also this code expects the 'forward' of the sprite to be to his right. If you are using the 'up' as forward, then this code will have to be changed. If you have a default setup with the character facing right and you are still having trouble, post the script you are using with this code and we can take a look.

The 3D Physics and 2D Physics don't mix. You can use both in the same program for different things, but a 2D object won't collide with an object with a 3D collider. All the 2D physics use Vector2s, so the 'Z' component is discarded/ignored.

Movement though Transform vs. CharacterController vs. Rigidbody are all different with their strengths and weaknesses, so which to use will depend on your app and goals. There is no universal "better." I encourage you to do a simple test app or work a tutorial with each.

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 Kensei · Jan 15, 2014 at 05:07 AM 0
Share

Thanks for the more in depth explanation of the commented code. I know the strengths and weaknesses of the movement type,I'm just not clear on the same topic but in 2D. Since there is no char controller in 2D, I'm wondering which is more efficient to work with. In my case with the cube, adding a rigidbody and then applying force to it doesn't move it at all, even when I pump the speed float to thousands. So my guess is that I would either need a slippery material for that.

Thing is that I would like to have a 3D mesh face the mouse on an orthographic camera. Is there a way to restrict the rotation only to x and y?

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

19 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

Related Questions

How to set up a compass for 2d game? 1 Answer

2D Platformer Character Controller 1 Answer

[2DMode] Targeting enemy and change direction 0 Answers

2d Texture Rotation Problem 1 Answer

2D Top down pathfinding 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