Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NinjaSquirrel · May 24, 2011 at 08:25 PM · 2dspritemouselook

2D mouselook

Does anyone know how I could perform a 2D sidescroller mouselook? Basically I want the sprit's arm and weapon to look at where the mouse is pointing. How can I do this? Any ideas?

Thanks.


Edit: What I've tried:

Place a collider object flush with sprites and raycast from the mouse position. Get the racast hit point and use the LookAt function on the arm.

Problems:

  • LookAt is a 3d function so a 1-sided "plane" mesh will not work well as there is no way to define the way the arm sprite faces when it looks at the point. This results in the visible side of the mesh not facing the camera.

  • The arm sprite is parented to the player so the arm's geometry is scaled to the player. This makes it hard to do complex angle movements I think?

  • The arm does not hinge properly at the elbow joint.

  • Due to the LookAt function being mainly for 3d uses unnecessary calculations are taking place. I only need to rotate the plane on the x and y.

Comment
Add comment · Show 3
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 GesterX · May 24, 2011 at 08:27 PM 0
Share

Is this for topdown or side-view?

avatar image NinjaSquirrel · May 24, 2011 at 08:40 PM 0
Share

Side view. The sprites move along the x and y axis.

avatar image GesterX · May 24, 2011 at 08:46 PM 0
Share

Oh that's outside of my knowledge :-(

4 Replies

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

Answer by NinjaSquirrel · May 25, 2011 at 09:46 PM

LookAt is a 3d function so a 1-sided "plane" mesh will not work well as there is no way to define the way the arm sprite faces when it looks at the point. This results in the visible side of the mesh not facing the camera.

I was able to fix this by simply rotating the sprite so that at runtime the rendered side is facing the proper way during the LookAt function.

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 Scribe · May 25, 2011 at 10:10 PM 0
Share

well done! +1 for common sense :)

avatar image
1

Answer by Scribe · May 25, 2011 at 09:20 PM

Hi there, so I don't work much with 2D sprites so I have only tested this on a camera and box but it worked so hopefully you can add and edit part for it to work for you.

This is the code I ended up with:

 var Sprite : Transform;
 var point1 : Vector3;
 
 function Update () {
     point1 = camera.ScreenToWorldPoint (Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));
     print(point1);
     
     Sprite.LookAt(Vector3(point1.x, point1.y, Sprite.position.z));
 }

My set up had my camera looking down the z axis so that only the x and y axis could be seen. I used the mouse input to convert the mouse point to a world point. I then made the sprite look at the point but only the x and y values of it. the z value that it looks at is its own z position so it never rotates in any direction other than along the z axis.

hope that helps

Scribe

Comment
Add comment · 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
-1

Answer by questionsforunity · May 24, 2011 at 08:32 PM

I think there is a world to 2d Point function.

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 GesterX · May 24, 2011 at 08:35 PM 2
Share

You should add a comment not a redundant answer...

avatar image FLASHDENMARK · May 24, 2011 at 08:39 PM 0
Share

@GesterX What you said.

avatar image NinjaSquirrel · May 24, 2011 at 08:47 PM 0
Share

I am asking how I should do this, not for code specifically.

avatar image
0

Answer by Patyrn · May 24, 2011 at 08:53 PM

This code from a mouse-drag camera ought to serve your purposes. Create a plane at the depth of your 2d stuff, and raycast to it. Use lookat on the hit point.

 mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 groundPlane.Raycast(mouseRay, out hitDist);
 Vector3 dragPoint = mouseRay.GetPoint(hitDist);

I noticed part of your problem with lookat was the nature of your sprites. I would recommend rotating your whole scene so that "forward" for the sprites is "right" from your viewport.

Comment
Add comment · Show 10 · 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 NinjaSquirrel · May 24, 2011 at 08:59 PM 0
Share

Please read the edit on the original post.

avatar image NinjaSquirrel · May 24, 2011 at 09:07 PM 0
Share

That seems like going out of my way a bit, but I think I will try that. Thanks for the help. :)

avatar image Patyrn · May 24, 2011 at 09:34 PM 0
Share

Yeah it's a bit counter-intuitive to restructure how you do things like that, but I have found the helper methods are useful enough to make it worth it.

avatar image NinjaSquirrel · May 24, 2011 at 11:09 PM 0
Share

Yes, but it's not a big deal as my project is fairly new and it should be easy to fix. :)

avatar image NinjaSquirrel · May 24, 2011 at 11:33 PM 0
Share

I believe LookAt uses the local rotation, so when I move the whole scene it has no effect. :(

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I achieve this particular 2D lighting effect? 2 Answers

2D LookAt not working as intended 1 Answer

Alpha cutoff of sprite based on another texture 1 Answer

2d sprite characters in a 3d game 1 Answer

Idle animation not working 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