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 /
  • Help Room /
avatar image
0
Question by kidne · Sep 12, 2016 at 03:52 PM · quaternionquaternion.lookrotationquaternion.slerp

Can't turn Y axis if terrain Hugging

I just recently applied code that would make it so that the character would terrain hug i.e. change angle according to the angle of the terrain underneath them. The problem is that I'm terrible with vector and angles and can't figure out why this takes precedent over my ability to rotate the character.

 Transform cameraTransform = Camera.main.transform;
             Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
             forward.y = 0;
             forward = forward.normalized;
 
             // Right vector relative to the camera
             // Always orthogonal to the forward vector
             Vector3 right = new Vector3(forward.z, 0, -forward.x);
             if (isControllable)
             {
                 v = Input.GetAxis("Vertical");
                 h = Input.GetAxis("Horizontal");
             }
 
 
             targetDirection = h * right + v * forward;
             // Smooth the speed based on the current target direction
 
             // Choose target speed
             //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
             targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0f);
             //moveDirection = Vector3.zero;
             Vector3 moveDirection = transform.TransformDirection(Vector3.forward);
             if (targetDirection != Vector3.zero)
             {
                 
                 moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
                 moveDirection = moveDirection.normalized;
             }
             float verticalSpeed = 0.0f;
             Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0);
             movement *= Time.deltaTime;

 // this line seems to control rotating and moving towards rotated direction
             transform.rotation = Quaternion.LookRotation(moveDirection);
 
             ////////////////////////////////////////////////////////ROTATE TOWARDS GROUND//////////////////////////////
             Ray ray = new Ray(transform.position, -transform.up);
             //RaycastHit hit;
             Physics.Raycast(ray,out hit);
             if(hit.normal != Vector3.zero)
             {
                 Quaternion newRot = Quaternion.FromToRotation (Vector3.up, hit.normal);
                 transform.localRotation = Quaternion.Slerp(transform.localRotation, newRot, 0.5f);
             }

If I have the "Rotate towards Ground" code at the very bottom of this function (called in Update) the character only walks with Y equaling 0 exclusively. It behaves properly aside from that.

However, if I move the rotating code underneath the 'Rotate towards ground' code, now I can rotate, but I don't angle towards the ground at all. I'm unsure how to keep the y axis from being affected but still allowing terrain hugging.

     ////////////////////////////////////////////////////////ROTATE TOWARDS GROUND//////////////////////////////
         Ray ray = new Ray(transform.position, -transform.up);
         //RaycastHit hit;
         Physics.Raycast(ray,out hit);
         if(hit.normal != Vector3.zero)
         {
             Quaternion newRot = Quaternion.FromToRotation (Vector3.up, hit.normal);
             transform.localRotation = Quaternion.Slerp(transform.localRotation, newRot, 0.5f);
         }

  // this line seems to control rotating and moving towards rotated direction
                 transform.rotation = Quaternion.LookRotation(moveDirection);


I've tried picking out only the z and x axis to be affected by ground hugging rotation but it gives weird results/no results at all.

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

0 Replies

· Add your reply
  • Sort: 

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

52 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

Related Questions

Getting a smooth rotation with Quaterminions slerp in IEnumerator 1 Answer

Enemy rotation while looking. Help. 0 Answers

Help with Quaternion.Slerp movement? 1 Answer

Uniform quaternion from a list of points? 0 Answers

[SOLVED] How to LookRotation in only Y axis 0 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