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 Nosferator · Jun 21, 2020 at 10:59 PM · rotationterrainnormals

Rotation Transform to face character AND normal to terrain

Hello, I would need a little help concerning the rotation of an object so that it is positioned at the normal of the ground but also towards the camera (or the character).

I'm really bad as soon as angles come into play ...

I think you will understand better with the following image:

image

on the left, what I managed to do with the following code:

 RaycastHit hit;
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 int layerMask = LayerMask.GetMask("Ground");
 if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
 {
     drag.transform.position = hit.point;
     drag.transform.up = hit.normal;
 }

on the right image what I wish I had.

The object is a child of the character, I don't know if that can help for the calculations.

I added a grid so that you can see the deformation of the terrain.

I hope I have managed to get you to understand me and I thank you very much for your help in advance

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

Answer by Namey5 · Jun 21, 2020 at 11:18 PM

The quaternion LookRotation constructor takes a forward vector and an optional up vector, so we can use that to set the object's rotation directly ('player' is just whatever object you want it to look at);

 RaycastHit hit;
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 int layerMask = LayerMask.GetMask("Ground");
 
 Vector3 up = Vector3.up;
 if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
 {
     drag.transform.position = hit.point;
     up = hit.normal;
 }
 
 Vector3 forward = (player.transform.position - drag.transform.position).normalized;
 
 drag.transform.rotation = Quaternion.LookRotation (forward, up);
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 Nosferator · Jun 22, 2020 at 05:11 PM

Thank you very much @Namey5 , it works perfectly. I just tweaked it a bit because I just wanted a direction not really "around" the character.

 RaycastHit hit;
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 int layerMask = LayerMask.GetMask("Ground");
 if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
 {
     drag.transform.position = hit.point;
     drag.transform.up = hit.normal;
 
     drag.transform.rotation = Quaternion.LookRotation(character.transform.forward, drag.transform.up);
 }

Do you think is possible to rotate the object on itself with a fixed angle but that it always keeps the same direction as in the previous script?

I would also like to bring the object out of the ground but I think I'll find something on the forum.

thank you very much again

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

193 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 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 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 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 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 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 avatar image

Related Questions

Bound the gameobject from the ground 1 Answer

Bake terrain normal via script 0 Answers

Making a particle effect parallel to the slope of a terrain 1 Answer

How to handler rotation of a Gameobject while aligning it with normals 1 Answer

hit.normal rotation problem, help please really confused 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