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 kubasteve · Jul 10, 2014 at 04:32 PM · rotationraycastlookatgroundperpendicular

Rotate object to lookAt point and be perpendicular to the ground

My game is a top down game, and to move the player object it just moves towards the mouse point. Right now I have it setup so that the object orients itself to face the point where the user clicked and the object moves to that point. However what I am having an issue with is having the player object face the mouse point and have the player object to correctly stay perpendicular to the ground. Any ideas how to do this?

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 Graham-Dunnett ♦♦ · Jul 10, 2014 at 04:34 PM 0
Share

Typically you'd rotate the player around the world up vector (which should be perpendicular to your ground).

avatar image kubasteve · Jul 10, 2014 at 05:31 PM 0
Share

What I tried doing is this:

 RaycastHit hitInfo;
 Vector3 lookAtPoint   = Vector3.zero;
 Vector3 localHitPoint = Vector3.zero;
     
 if(Physics.Raycast(r, out hitInfo, 1000.0f, gameInput$$anonymous$$ask))
 {
     RaycastHit hit; 
     if (Physics.Raycast(physics$$anonymous$$art.transform.position, -physics$$anonymous$$art.transform.up, out hit, 10.0f, ramp$$anonymous$$ask))                        
     {
         physics$$anonymous$$art.transform.localPosition = rootTransform.InverseTransformPoint(hit.point);
     }
     localHitPoint = rootTransform.InverseTransformPoint(hitInfo.point);
      Vector3 delta = localHitPoint - physics$$anonymous$$art.transform.localPosition;
 
     lookAtPoint = new Vector3(localHitPoint.x, physics$$anonymous$$art.transform.localPosition.y, localHitPoint.z);

     physics$$anonymous$$art.transform.LookAt(rootTransform.TransformPoint(lookAtPoint, hit.normal));
 }


So the first raycast is to deter$$anonymous$$e where the mouse position is (so this is what I want to face). The second raycast within is to deter$$anonymous$$e what we are standing on. I plug this all into my physics$$anonymous$$art object using LookAt and I use the hit.normal to base the world's up vector.

avatar image robertbu · Jul 10, 2014 at 05:46 PM 0
Share

Your code is hinting at a complicated setup that I don't understand. The typical way of fixing this problem is to move your hit.point to the level of the player before doing the LookAt(). Note if your plane that the character is walking on is perfectly flat, you can use Unity's mathematical Plane class and Plane.Raycast() the point. If the mathematical plane is constructed so that it passes through the pivot point of the player, then your LookAt() problem will go away.

1 Reply

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

Answer by PiraLezz · Jul 11, 2014 at 12:20 PM

use this method:

 transform.LookAt();

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

SphereCast help 1 Answer

Make character look at mouse position, not world position 3 Answers

LookAt To Only Rotate on Y Axis 2 Answers

Top down look rotation snaps when looking left, right, and back 1 Answer

[HELP]Object Look At Raycast position 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