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 TurboHermit · Mar 07, 2012 at 11:48 PM · rotationmouselookattargetmouse position

Object Look At Mouse

I've seen a lot of similar questions, but none seem to give me the wanted effect. I have an object in my scene that I want to point at the position of my mouse. Except, it's 2d gameplay so I just want it to point at the x and the y coordinates of my mouse. I've tried different approaches, but it didn't really work out the way I'd liked it too.

Anyone any suggestions?

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
Best Answer

Answer by Berenger · Mar 08, 2012 at 12:00 AM

If it is a top-down game, you can use Camera.main.ScreenToWorld. This gives you a point to use with LookAt, or by affecting the direction object -> point to the object's forward vector. Just ignore the axe you don't use.

Comment
Add comment · Show 6 · 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 TurboHermit · Mar 09, 2012 at 04:22 PM 0
Share

And how would I use Camera.ScreenToWorldPoint to get the position of the mouse?

avatar image Berenger · Mar 09, 2012 at 04:29 PM 0
Share

Camera.ScreenToWorldPoint(Input.mousePosition) gives you the 3D position of the mouse on the near plane. This is relevant only if the camera is orthograpic. If it's perspective, you will need to cast a ray with Physics.Raycast or Plane.Raycast and Camera.ScreenPointToRay.

avatar image TurboHermit · Mar 09, 2012 at 05:17 PM 0
Share

This looks like it's going the right way, my camera is orthographic and I'm trying to use this piece of code:

var targetmouse : Camera.ScreenToWorldPoint(Input.mousePosition);

ship.transform.LookAt(targetmouse);

Yet somehow I get this error "UCE0001: ';' expected. Insert a semicolon at the end."

avatar image Berenger · Mar 09, 2012 at 05:30 PM 0
Share

Almost there. What comes after the : in a var declaration is the type, not the value. The type here is Vector3. Use = to affect the value.

Note that the position you'll get that way has approximately the same y than your camera (if it's looking down) and the ship doesn't, so it's going to look up and I assume you don't want that. You need to add targetmouse.y = ship.transform.position.y to be perfectly 2d. That's what I meant by "Just ignore the axe you don't use"

avatar image TurboHermit · Mar 09, 2012 at 05:45 PM 0
Share

So this is the code I'm currently using: var targetmouse : Vector3 = Camera.ScreenToWorldPoint(Input.mousePosition);

 targetmouse.x = ship.transform.position.x;

 ship.transform.LookAt(targetmouse);

But now it's giving me this error: BCE0020: An instance of type 'UnityEngine.Camera' is required to access non static member 'ScreenToWorldPoint'.

Show more comments
avatar image
1

Answer by Taylor-Libonati · Aug 05, 2012 at 08:55 PM

I had a similer problem with my top down game. In my case x is left and right, y is up and down, so I wanted the rotation to be happening around the z axis. If you have a different orientation you will have to edit some of the code.

Here was my solution:

 //Aim player at mouse
  //which direction is up
  Vector3 upAxis = new Vector3(0,0,-1);
  Vector3 mouseScreenPosition = Input.mousePosition;
  //set mouses z to your targets
  mouseScreenPosition.z = transform.position.z;
  Vector3 mouseWorldSpace = Camera.mainCamera.ScreenToWorldPoint(mouseScreenPosition);
  transform.LookAt(mouseWorldSpace, upAxis);
  //zero out all rotations except the axis I want
  transform.eulerAngles = new Vector3(0,0,-transform.eulerAngles.z);
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 RoguPictur · Dec 30, 2013 at 08:17 PM 0
Share

Awesome this worked for me thanks !

avatar image Jonathan-Bro · Feb 10, 2014 at 04:11 AM 0
Share

Thank you so much! I tried everything else, but this worked perfect

avatar image IAmCraigSnedeker · Aug 13, 2014 at 08:49 PM 0
Share

This is the first script that I found that works, but it only rotates about 20 degrees in each direction.

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

9 People are following this question.

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

Related Questions

Lock rotation axis? 4 Answers

Gun should point at mouse position 3 Answers

Having a first person player look at the mouse on the y axis 1 Answer

Third person animation 0 Answers

transform.LookAt gives a twist 2 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