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 mtGameDev · Oct 23, 2021 at 11:16 AM · slope

Rotate gameobject accroding to ground slope ?!

i have a horse animal that is controllable. i want to set animal rotation with ground slope. i've use some codes but doesn't work. so please help me to fix this.

i've used this code :

 RaycastHit hit;
 if (Physics.SphereCast(transform.position,0.5f, -(transform.up), out hit, 1))
 {
     transform.rotation = Quaternion.LookRotation(Vector3.Cross(transform.right, hit.normal));
 }
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

Answer by Pangamini · Oct 25, 2021 at 07:26 PM

Try

transform.rotation = Quaternion.LookRotation(transform.right, hit.normal);

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 andrew-lukasik · Oct 25, 2021 at 11:06 PM 0
Share

Good general direction, but that transform.right turns a humble horse into an Apache attack helicopter :D AH-64

avatar image Pangamini andrew-lukasik · Oct 26, 2021 at 08:02 AM 0
Share

I got that from your code. It would make more sense to me to use transform.forward, or better, some stable solution that doesn't use transform to change itself

avatar image
0

Answer by andrew-lukasik · Oct 25, 2021 at 11:08 PM

rotate GameObject according to ground slope

RotateGameObjectAccordingToGroundSlope.cs

 using UnityEngine;
 public class RotateGameObjectAccordingToGroundSlope : MonoBehaviour
 {
     Vector3 _dir = Vector3.forward;
     Camera _camera = null;
     void Start ()
     {
         _dir = transform.forward;
         _camera = Camera.main;
     }
     void Update ()
     {
         Vector3 inputXZ = _camera.transform.right*Input.GetAxis("Horizontal") + _camera.transform.forward*Input.GetAxis("Vertical");
         inputXZ.y = 0;
         if( inputXZ.sqrMagnitude>0 ) _dir = inputXZ.normalized;
 
         var ray = _camera.ScreenPointToRay( Input.mousePosition );
         if( Physics.Raycast( ray , out var hit ) )
         {
             Vector3 tangent = Vector3.ProjectOnPlane( _dir , hit.normal );
             transform.rotation = Quaternion.LookRotation( tangent , hit.normal );
 
             transform.position = hit.point;
             Debug.DrawRay( hit.point , hit.normal , Color.yellow );
             Debug.DrawRay( hit.point , tangent , Color.cyan );
             Debug.DrawRay( hit.point , inputXZ , Color.magenta );
         }
         
         // var ray = new Ray( transform.position , Vector3.down );
         // if( Physics.Raycast( ray , out var hit , 1 ) )
         // {
         //     Vector3 tangent = Vector3.ProjectOnPlane( _dir , hit.normal );
         //     transform.rotation = Quaternion.LookRotation( tangent , hit.normal );
         // }
     }
 }

NavMeshAgent horse controller Here is a script that allows you to control this "horse" with a keyboard input. It requires NavMeshAgent component and a valid nav mesh.

tip: lower the Angular Speed on that NavMeshAgent component to something around 3.0. Default 120 is too much here.

> NavMeshAgentHorseController.cs

 using UnityEngine;
 using UnityEngine.AI;
 
 public class NavMeshAgentHorseController : MonoBehaviour
 {
     [SerializeField] NavMeshAgent _navComponent  = null;
     Vector3 _moveDirXZ, _moveNormal, _moveTangent;
     void Start ()
     {
         _moveDirXZ = transform.forward;
         _moveNormal = Vector3.up;
     }
     void Update ()
     {
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         
         _moveDirXZ = Quaternion.Euler( 0 , h * _navComponent.angularSpeed , 0 ) * _moveDirXZ;
         _navComponent.velocity = Vector3.MoveTowards(
             _navComponent.velocity ,
             _moveDirXZ * v * _navComponent.speed ,
             _navComponent.acceleration * Time.deltaTime
         );
     }
     void LateUpdate ()
     {
         if( Physics.Raycast( transform.position , Vector3.down , out var hit , 1f , _navComponent.areaMask ) )
         {
             Vector3 tangent = Vector3.ProjectOnPlane( _moveDirXZ , hit.normal ).normalized;
             _moveTangent = Vector3.Lerp( _moveTangent , tangent , Time.deltaTime*5f );
             _moveNormal = Vector3.Lerp( _moveNormal , hit.normal , Time.deltaTime*5f );
             transform.rotation = Quaternion.LookRotation( _moveTangent , _moveNormal );
 
             Debug.DrawRay( hit.point , hit.normal , Color.yellow );
             Debug.DrawRay( hit.point , _moveDirXZ , Color.red );
         }
     }
 }

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 mtGameDev · Oct 27, 2021 at 08:05 PM 0
Share

Thanks for your code. I think that it's a proper code but I have separated MainCamera and the Horse is controlled by the keyboard. so mouse inputs and camera transform and input.getAxis is caused that it doesn't work properly. could you please write a code that is suitable for my game ?

avatar image andrew-lukasik mtGameDev · Oct 27, 2021 at 11:06 PM 0
Share

I added additional script in my answer, NavMeshAgentHorseController.cs.

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

131 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

Related Questions

Help with sliding down hills 1 Answer

Ramp for stairs causes player to slide down. 1 Answer

How can I make my FPS character slide down a angle. 0 Answers

How should I rotate my character on a slope? (2d game) 0 Answers

How can I guarantee a CharacterController never leaves the ground? 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