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 alexanderflink · Oct 16, 2014 at 11:38 AM · camerarotationraycastlookat

Make character look at mouse position, not world position

Hello, I want my character to always face the mouse position. This is a isometric type game.

This is how i've done it now, in the Update function:

     //aiming
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray,hit,1000)) {
         Debug.DrawLine (ray.origin, hit.point,Color.green);
         aimPos = Vector3(hit.point.x,upperBody.position.y,hit.point.z);
     }
     upperBody.LookAt(aimPos);

This sort of works, however. The character will look at objects sticking up out of the ground, as the camera ray hit's them. This results in the player not being able to look parallel to a wall for example, since they will either look AT the wall, or at the ground beside it.

I know that I can put the objects that I don't want the ray to collide with on an IgnoreRayCast layer, however, that creates another problem. If the character climbs up on a wall, and that wall is on ignore raycast, if the cursor is put below the character, it will still look up (away from the camera), since the ray from the camera passes through the wall that the character is standing on, and lands in front of the character.

What I want is basically: Whenever the cursor is below the character, it is supposed to look down. When it is above the character, it is supposed to look up, same things goes for left and right.

Could anyone please point me in the right direction?

Thanks. /Alex

EDIT: Below is a screenshot of the issue. As you can see, the mouse cursor is below the character, but it doesn't look towards the mouse, since the ray shoots through the object that the character is standing on. The objects need to ignore the raycast, since I always want the character to look at the mouse position, not at the objects. (The legs rotate independently of the upper body, they are irrelevant to this issue)

alt text

scr.jpg (69.4 kB)
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 jkpenner · Oct 17, 2014 at 02:02 AM 0
Share

Could you possible flip how you're doing your calculations. Ins$$anonymous$$d of going from screen to world, go from world to screen. Something like the following:

 Vector3 upperBodyScreenPos = Camera.main.WorldToScreenPoint(upperBody.position);
 Vector3 upperBodyToCursor = Input.mousePosition - upperBodyScreenPos;
 upperBody.rotation = Quaternion.LookRotation(upperBodyToCursor, Vector3.Up);

I'm not sure if this exact code would work, but maybe someone could extend upon the idea.

avatar image KayelGee · Oct 17, 2014 at 08:27 AM 0
Share

Your description is quite confusing. Could you make some pictures to explain what you mean and how it should work?

avatar image alexanderflink · Oct 17, 2014 at 10:49 AM 0
Share

Thanks jkpenner, I tried your method, it seems logical. However, the character looked up in the sky, and didn't face the mouse cursor correctly. I tried setting upperBodyToCursor.y = 0; , but it still didn't look correctly at the mouse cursor. I can't seem to figure out why...

3 Replies

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

Answer by alexanderflink · Oct 21, 2014 at 10:56 AM

Hello! I managed to get it right, this is how I did it. Thanks cmonroy and jkpenner for explaining how the logic should be. Note how I use upperBodyToCursor.y instead of z, since z is the distance from the camera, not direction to mouse cursor.

 //aiming
     //get the screen position of the upper body bone
     upperBodyScreenPos = Camera.main.WorldToScreenPoint(upperBody.position);
     
     //get the direction from the upper body to cursor
     upperBodyToCursor =  Input.mousePosition - upperBodyScreenPos;
     
     //set aimPos to upperbody position + direction vector
     aimPos = (upperBody.position + Vector3(upperBodyToCursor.x,upperBody.position.y,upperBodyToCursor.y));
     
     // transform aimPos to camera space
     finalAimPos = cameraDirTransform.TransformDirection(Vector3(aimPos.x,0,aimPos.z));
     
     //look at aimpos
     upperBody.LookAt(finalAimPos);
     
     //calculate movement relative to camera
     finalMovement = cameraDirTransform.TransformDirection(Vector3(horizontalInput,0,verticalInput));
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
0

Answer by Christian.Tucker · Oct 17, 2014 at 09:04 AM

Use WorldToScreenPoint instead of ScreenPointToRay

             Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
             RaycastHit hit;
             Vector3 direction;
             Quaternion rotation;
             if(Physics.Raycast (ray, out hit, 5f)) 
             {
                 direction = (hit.point - transform.position).normalized;
             }
             else 
             {
                 direction = ray.direction;
             }
             
             rotation = Quaternion.FromToRotation(transform.forward, direction);




This will shoot a raycast out from the center of your screen, it will then perform calculations to get the center of the screen if it hits something, and if it doesn't it will simply turn towards the direction of the ray.

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 alexanderflink · Oct 17, 2014 at 10:37 AM 0
Share

Thank you for you answer, i'm not sure im following though. I can't see any "WorldToScreenPoint" function in your code?

Also, this creates a ray from the center of the viewport, not from the mouse position. How would I go about shooting a ray from the mouse position this way? Thanks!

avatar image
0

Answer by cmonroy · Oct 18, 2014 at 10:04 AM

The origin of the ray should be located at your character's location. The mouse position should be the end destination of the ray. The problem as I see it is that even though you have a definite X and Y values, the required Z (depth) is estimated for any geometry available at that spot and may not always be what you expect.

You could, however, create an invisible object at a fixed distance from your character (let's say 2 meters) and provide the X and Y values from your Mouse position. Having the X, Y and Z components, just transform the position of the target and simply look at it.

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

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

32 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 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

Best way to tell which object(s) are being looked at? 3 Answers

Have An Object Rotate Around Transform A So That Transform B is Looking at Transform C (C#) 0 Answers

How to rotate bone relative to camera using LookAt 3 Answers

Changing The Camera Position & Offsetting LookAt 2 Answers

Camera rotate to look at GameObject from Raycast 3 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