Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Hoorza · Nov 30, 2018 at 12:21 AM · navmeshagentraycastingnavigationnormals

How to rotate NavMesh Agent to the surface it is walking on

Hello . I am trying to get my NPC to follow the shape of the surface he is walking on as it travels towards its target (for example: lean forward when it goes down the hill. I do raycasting to read normals of the surface, but when I try to implement that data in he will not rotate on Y axis anymore ( NPC not looking on its target anymore). It will respond to the curvature of the surface but won't rotate as he turns. So I know how to do both separately but I can't manage to get it to work together. It is a mobile game so it has to be very simple. Any help with the code would be deeply appreciated, even if someone knows a simple asset that I could purchase to solve that problem! Thanks a lot. Here is the code for grabbing surface normals:

 `        
    
 
     downward = transform.TransformDirection(Vector3.down) * 1;
     Debug.DrawRay(transform.position, downward, Color.green);

     if (Physics.Raycast(transform.position,(downward), out hit, 1, mask))
     {
         distance = hit.distance;
         sheepBody.transform.position = hit.point;
         sheepBody.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);`


The way I understand it, what I need to do is to alter X and Z-axis for a navmesh but not the Y. If you know of a better way I am open. Cheerio!!

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

Answer by Hoorza · Nov 30, 2018 at 05:13 PM

I got it! For those looking for a solution to this problem. NaveMesh agent is placed on the parent object. Next, create child object that contains meshes and the script with the code below. And VOILA! So far my object has no animations and it works just fine, also I do not have very steep terrain in my game, so can't tell if it works well with the steeper surface. Cheerio!!

 using UnityEngine;
 
 public class SheepTerrainAlign : MonoBehaviour
 { 
     RaycastHit hit;
     Vector3 theRay;
 
     public LayerMask terainMask;
 
     void FixedUpdate()
     {
         Align();
     }
   
     private void Align()
     {
         theRay = -transform.up;
 
         if (Physics.Raycast(new Vector3(transform.position.x, transform.position.y, transform.position.z),
             theRay, out hit, 20, terainMask))
         {
 
             Quaternion targetRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.parent.rotation;
 
             transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime / 0.15f);
         }
     }
 }
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 Elizeuvix · Nov 07, 2021 at 12:28 PM

How to rotate object in NavmeshAgent but using WASD keys?

@Hoorza

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 MaxGeorg · Dec 15, 2021 at 11:27 AM 0
Share

https://answers.unity.com/questions/1346988/how-can-i-use-a-navmesh-with-keyboard-input-to-mov.html

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

165 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

Related Questions

NavMesh pathing not working 0 Answers

Second NavMesh Surface refuses to cooperate: Failed to create agent because it is not close enough to the NavMesh 0 Answers

Problem with navagent killing FPS 0 Answers

Using HeightMesh for navigation 0 Answers

Is there a way to have a NavMeshAgent ignore inclines? 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