NavmeshAgent 播放器在山上移动时与山坡不平行 [英] NavmeshAgent player not parallel to slope of hill when moving over hill

查看:25
本文介绍了NavmeshAgent 播放器在山上移动时与山坡不平行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NavmeshAgent 播放器在山上移动时不平行到山坡.在平面上它进行得很顺利.

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

观看视频

下面图像 navMesh 和播放器的属性https://ibb.co/fijmoV

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);

}

}

推荐答案

我不确定 NavmeshAgent 是否应该为您做到这一点.这看起来像是您应该手动执行的操作.

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

您可以通过向下执行光线投射并获得命中点的法线来校正角色的旋转以匹配斜率.获得命中点的法线后,您可以使用该法线命中点计算新的旋转.有很多方法可以进行该计算,但使用 Quaternion.FromToRotation 并使用 Quaternion.Lerp 进行旋转似乎效果最好.

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.

最后,确保仅对您认为是山"或地"的对象进行光线投射.您可以通过对放置Hill"或Ground"对象的图层进行按位运算来完成此操作.下面的示例假设您认为是山"或地"的对象位于名为山"的图层上.

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);

    }

}

这篇关于NavmeshAgent 播放器在山上移动时与山坡不平行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆