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 encom · Feb 15, 2012 at 02:42 AM · lookattop-down

Top-Down LookAt problem

Hello there,

I am new to Unity and Javascript and am having problems understanding some fundamental concepts. I have watched as many tutorials as I could find and scoured these forums. Although I have found many questions about "Top-down Rotation" no answers have really helped me so please forgive me if this has been answered before.

I have a simple stage with my character on it and a fixed camera facing down. What I am trying to figure out is how to (simply) have my character face the mouse cursor, rotating around the Y-axis.

This is the script I have figured out so far:

 var mouseX : int;
 var mouseY : int;
 
 function Update ()   {
 
 mouseX = Input.mousePosition.x;
 mouseY = Input.mousePosition.y;
 
 transform.LookAt(Vector3(mouseX,0,mouseY));
 }

My character does rotate a bit, but not 360 degrees. I'm just trying to figure out what I'm doing wrong here. Any explanation on how the code works would be greatly appreciated as I'm having trouble grasping the fundamentals here (boy i feel like a noob).

Anyway, thanks in advance for your answers.

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
0

Answer by Berenger · Feb 15, 2012 at 03:40 AM

You are confusing two different spaces here. Input.mousePosition is in screen space, between (0,0,0) and (Screen.width, Screen.height, 0), when your player with the Transform component is in world space, which is unlimited (kind of). Now, You have two ways (that I think of now) to do what you want to achieve, to find the direction. It must be done in a single space.

  • Screen space : Assuming your character is in the middle of the screen, the movement direction will be Input.mousePosition - M, with M = (Scree.width0.5, Screen.height0.5). Then, the direction will be (result.x, 0, result.y)
  • World space : you need the world position of your mouse. Create the ray with Camera.ScreenPointToRay, then find the intersection with the ground / a plane facing up. The direction will be result - transform.position.

Good luck.

OH, and you should Quaternion.SetLookRotation if you use a direction (or look at position + direction)

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 encom · Feb 15, 2012 at 07:44 PM 0
Share

Awesome dude thank you

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

6 People are following this question.

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

Related Questions

How do I make an object look at an another object in Top-Down 2D? ( C# ) 1 Answer

Waypoint navigation in 2D without LookAt 2 Answers

Respawn looking 1 Answer

Aim flashlight at cursor in third person 1 Answer

how do i make somthing rotate with mouse 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