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 justaleaf · Dec 01, 2013 at 05:52 AM · mouselookaim

Object "Look at" Mouse Z Axis

All I'm trying to do, is to get an object to rotate on it's Z axis to "look at" the current mouse position. The camera is orthographic and the object in question does not need to move or rotate in any other way. This camera does not move either.

I need this as an aim mechanic for a Bust-a-move style game project.

alt text

What is the easiest way to do this? Every script I've attempted to use has thrown errors for me.

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

1 Reply

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

Answer by Tomer-Barkan · Dec 01, 2013 at 05:55 AM

Attach this script to the object you want to rotate:

public class MouseRotator : MonoBehaviour {

 public void Update() {
     Vector3 mousePositionInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     Vector3 toMousePosition = mousePositionInWorld - transform.position;
     transform.up = toMousePosition;
 }

}

The first line calculates the world position of the mouse pointer. The second line calculates the vector that points from the object's position to the mouse's position. The third line sets the object's up direction, so that it points to from the object to the mouse, causing the object to rotate.

Comment
Add comment · Show 4 · 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 justaleaf · Dec 02, 2013 at 06:30 PM 0
Share

It's throwing the following error. Do I need to assign the camera, somehow?

Assets/Lookat$$anonymous$$ouse2.cs(13,60): error CS1061: Type UnityEngine.Camera' does not contain a definition for ScreenToWorldPosition' and no extension method ScreenToWorldPosition' of type UnityEngine.Camera' could be found (are you missing a using directive or an assembly reference?)

avatar image Tomer-Barkan · Dec 02, 2013 at 07:06 PM 0
Share

$$anonymous$$y Typo, it's ScreenToWorldPoint, not ScreenToWorldPosition. Updated answer.

avatar image justaleaf · Dec 03, 2013 at 07:24 AM 0
Share

Works perfect. Thank you!

avatar image Tomer-Barkan · Dec 03, 2013 at 07:33 AM 0
Share

Good to hear. Please remember in Unity Answers you should accept the answers that solved your problem (clicking the check icon under the vote up/vote down to the left of the answer)

Good luck.

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Selecting scene for your main menu 1 Answer

Mouse Look Adapt 1 Answer

Aim Assist Script Problems 0 Answers

MouseLook script that points +Y at mouse and rotates on Z 1 Answer


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