Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Giddy_Up · Jun 26, 2018 at 11:24 AM · rotationtransformrotate

Setting rotation of game object relative to ground does not work

I have set up a humanoid moving around the game world. On right click its move point will change, will start walking towards it and it should rotate towards its target. But I also wanted to take into account terrain curvature, so I rotate the object in x and z axis based on terrain normal and rotate the object on y axis based on the target.

 protected void Move()
     {
         Vector3 heading = new Vector3();
         heading = (movePoint - this.transform.position);
 
         Ray ray = new Ray(transform.TransformPoint(GetComponent<BoxCollider>().center), -transform.up);
         RaycastHit hit;
 
         if (Physics.Raycast(ray, out hit, GetComponent<BoxCollider>().size.y, terrainLayerMask))
         {
             float rotStep = rotSpeed * Time.deltaTime;
             Vector3 objectForward = transform.TransformDirection(Vector3.forward);
             Vector3 newDir = Vector3.RotateTowards(objectForward, heading, rotStep, 0f);
             transform.rotation = Quaternion.Euler (Quaternion.LookRotation(objectForward, hit.normal).eulerAngles.x, Quaternion.LookRotation(newDir, hit.normal).eulerAngles.y, Quaternion.LookRotation(objectForward, hit.normal).eulerAngles.z);
         }
         float moveStep = moveSpeed * Time.deltaTime;
         Vector3 tempPos = Vector3.MoveTowards(transform.position, movePoint, moveStep);
         transform.position = new Vector3(tempPos.x, GetTerrainHeight(), tempPos.z);
     }

The code that calculates the rotation and sets it is inside the if (Physics.Raycast) part.

First I assign object forward transform in a variable "objectForward".

Then I calculate rotation towards the target and store it in "newDir".

After that I set objects rotation. The problem is, the rotation around the x axis is not setting, it stays on 0 all the time. The y and z axis are set up properly. Y rotation is based on target position, z rotation is based on terrain normal and x is also based on terrain normal but it's not working even though I use the same function for both of those variables.

I've suspected that i've used a wrong function for calculating x rotation so I've used "Debug.Log(Quaternion.LookRotation(objectForward, hit.normal).eulerAngles.x)" to check if it actually has a value and it does so I have no idea why it's not working properly.

Comment
Add comment · Show 1
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 tormentoarmagedoom · Jun 26, 2018 at 11:26 AM 0
Share

Good day.

As a recomendation, try to simplify your question if want an answer... i have no energy to decode what your script does...I'm sorry, maybe some other person does ($$anonymous$$aybe Hellium or Bunny) Try to extrapolate the problem to a simple case... or say whats the exatcly problem :D

1 Reply

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

Answer by ModLunar · Jun 26, 2018 at 11:34 AM

This is not a complete answer, but one thing that helps me out with rotations and transforms is their right, up, and forward properties. You can let Unity handle the rotation madness internally, and use something like:

 transform.up = hit.normal;

These will be in world space by the way. I hope this helps :)

Comment
Add comment · Show 2 · 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 Giddy_Up · Jun 26, 2018 at 11:49 AM 1
Share

It seems to work, although in this particular situation it creates some problems which I'll have to try and iron out, it shouldn't be too hard though.

avatar image ModLunar Giddy_Up · Jun 26, 2018 at 11:53 AM 0
Share

Oh I see, good luck! Perhaps you can also interpolate from the current up direction towards the hit.normal so it's a little smoother:

 transform.up = $$anonymous$$athf.Lerp(transform.up, hit.normal, 0.4f);

Depending on your situation. Ah well, goodluck!

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

136 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

Related Questions

How i can equal 2 Tranform Rotations? 1 Answer

My object isn't rotating to the transform of another object. How do I fix this? 1 Answer

What's wrong with my script? 1 Answer

How to prevent Z axis rotation ? 1 Answer

Find left joystick Vector2 away from sprite's up direction. 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