【问题标题】:NavmeshAgent player not parallel to slope of hill when moving over hillNavmeshAgent 播放器在翻山时与山坡不平行
【发布时间】:2018-11-02 11:20:02
【问题描述】:

NavmeshAgent 玩家在翻山时不平行到山坡。在平面上它进展顺利。

See Video

下面是导航网格和播放器的图像属性 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);

}

}

【问题讨论】:

  • 将对象接地不是导航网格代理的工作,您可以将刚体组件添加到游戏对象并应用重力,然后您的对象将被接地。
  • 如果我使用刚体重力,它会向下倾斜。它不会瞄准

标签: c# unity3d navmesh


【解决方案1】:

我不确定NavmeshAgent 是否应该为您执行此操作。这看起来像是您应该手动执行的操作。

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

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

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

    }

}

【讨论】:

  • 再次查看我的问题我添加了我的代码,请帮助我
  • 我回滚了您的编辑以显示我的编辑。请注意,我的编辑显示了动画 gif 的问题所在。把它留在那里。没有它,就无法判断问题出在哪里,因为您只链接到视频。此外,请停止使用您当前用于上传图片的网站,因为图片会在 x 小时内消失。请改用 Imgur,然后在此处链接到它。
  • 我不知道为什么你在这个答案中根本没有尝试过任何东西时仍然寻求帮助?阅读此答案的内容,然后尝试。将结果返回给我
  • 我使用 3D 平面并将其旋转以获得坡度,然后我为代理添加了 3D 立方体。立方体需要使用刚体吗? ,问题已编辑。
  • 您是否阅读了我的最后一条评论,即您应该停止删除显示您的问题的动画图像?人们不喜欢在外部网站上观看视频,例如您链接的网站。此外,您当前的编辑根本没有显示任何尝试使用我的答案中的代码。
猜你喜欢
  • 2021-11-03
  • 2022-11-17
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 2015-03-24
相关资源
最近更新 更多