-1

NavmeshAgent player not parallel to slope of hill when moving over hill. On plane surface its going smoothly.

See Video

Below Image properties of navMesh and player https://ibb.co/fijmoV

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class SampleAgentScript : MonoBehaviour {
public Transform  target ;
NavMeshAgent agent;
//  private static bool start1=false , start2=false, start3;
// Use this for initialization

void Start()
{
    agent = GetComponent<NavMeshAgent>();
}



void Update()
{

    //if white button click moves to targer-1

    agent.SetDestination(target.position);

}

}
Every Thing
  • 333
  • 2
  • 12
  • Grounding an objects isn't the nav mesh agents job, you can add a rigid-body component to your game object and apply gravity then your object will be grounded. – Milad Qasemi Nov 03 '18 at 08:39
  • if I used Rigid Body Gravity its going down side of slope. It is not going to target – Every Thing Nov 03 '18 at 09:56

1 Answers1

5

I am not sure if NavmeshAgent is supposed to do that for you. This looks like something you're supposed to do manually.

You can correct the rotation of the character to match the slope by performing a raycast downwards and obtaining the normal of the hit point. After obtaining the normal of the hit point, you can then calculate the new rotation with that normal hit point. There are many ways to do that calculation but using Quaternion.FromToRotation and lerping the rotation with Quaternion.Lerp seems to work best.

Finally, make sure to the raycast is only done to Objects you considered as a "Hill" or "Ground". You can do this with the bitwise operation on the layer the "Hill" or "Ground" objects are placed on. The example below assumes that the Objects you consider as "Hill" or "Ground" are on a layer called "Hill".

//Reference of the moving GameObject that will be corrected
public GameObject movingObject;

//Offset postion from where the raycast is cast from
public Vector3 originOffset;
public float maxRayDist = 100f;

//The speed to apply the corrected slope angle
public float slopeRotChangeSpeed = 10f;

void Update()
{
    //Get the object's position
    Transform objTrans = movingObject.transform;
    Vector3 origin = objTrans.position;

    //Only register raycast consided as Hill(Can be any layer name)
    int hillLayerIndex = LayerMask.NameToLayer("Hill");
    //Calculate layermask to Raycast to. 
    int layerMask = (1 << hillLayerIndex);


    RaycastHit slopeHit;

    //Perform raycast from the object's position downwards
    if (Physics.Raycast(origin + originOffset, Vector3.down, out slopeHit, maxRayDist, layerMask))
    {
        //Drawline to show the hit point
        Debug.DrawLine(origin + originOffset, slopeHit.point, Color.red);

        //Get slope angle from the raycast hit normal then calcuate new pos of the object
        Quaternion newRot = Quaternion.FromToRotation(objTrans.up, slopeHit.normal)
            * objTrans.rotation;

        //Apply the rotation 
        objTrans.rotation = Quaternion.Lerp(objTrans.rotation, newRot,
            Time.deltaTime * slopeRotChangeSpeed);

    }

}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • see my question again I added my code, please help me in that – Every Thing Nov 08 '18 at 09:17
  • 1
    I rolled back your edit to show my edit. Notice that my edit shows what the problem is with an animated gif. Leave that there. Without that there's no way to tell what the problem is since you only linked to a video. Also, stop using the website you're currently using to upload your images because the image disappears within x hours. Use Imgur instead then link to it here. – Programmer Nov 08 '18 at 14:20
  • I am not sure why you're still asking for help when you haven't tried anything in this answer at-all? Read the content of this answer then try it. Get back to me with the result – Programmer Nov 08 '18 at 14:21
  • I used 3D plane and rotate it for to get slope, then I have added 3D cube for agent. Is I need to used Rigid Body for cube? , question edited. – Every Thing Nov 09 '18 at 05:11
  • Did you read my last comment that says you should stop removing the animated image that shows your problem? People don't like watching videos on external sites, like the one you linked. Also, your current EDIT did not show any attempt to use the code from my answer at-all. – Programmer Nov 09 '18 at 06:19