【问题标题】:sphere cast outputting only the center of the game object球体投射仅输出游戏对象的中心
【发布时间】:2021-09-07 10:35:30
【问题描述】:

我正在尝试制作一个瞄准机器人,它可以通过投射的球体检测它是否击中半径更大的物体,如果瞄准机器人开启,那么它会在那里射击子弹,但它会一直输出命中的游戏对象的中间 spherecast() 它在更新中被调用 瞄准游戏对象它表示如果瞄准,子弹应该到达的位置 代码:

public void spherecast()
    {
        origin = gunTip.transform.position;
        direction = guntip.transform.forward;
        RaycastHit hit;
        if (Physics.SphereCast(origin,sphereradus, direction,out hit,maxDistance,whatIsenemy))
    {
        Debug.Log("I hit enemy");
        Debug.Log("position" + hit.transform.position);
        aim.transform.position = hit.transform.position;
        currentHitDistance = hit.distance;
    }
    else
    {
        currentHitDistance = maxDistance;
    }
}

【问题讨论】:

    标签: c# unity3d game-physics physics


    【解决方案1】:

    Physics.SphereCast 根本不会输出类似的内容。

    是你在使用hit.transform.position,这当然是命中对象的一般轴心点。

    SphereCast 为您提供RaycastHit 以及许多附加信息,尤其是您正在寻找的RaycastHit.point

    public void spherecast()
    {
        origin = gunTip.transform.position;
        direction = guntip.transform.forward;
    
        if (Physics.SphereCast(origin, sphereradus, direction,out var hit, maxDistance, whatIsenemy))
        {
            // By passing in the hit object you can click on the log in the console 
            // and it will highlight the according object in your scene ;)
            Debug.Log($"I hit enemy \"{hit.gameObject}\"", hit.gameObject);
    
            Debug.Log($"hit position: {hit.point}", this);
            aim.transform.position = hit.point;
            currentHitDistance = hit.distance;
        }
        else
        {
            currentHitDistance = maxDistance;
        }
    }
    

    【讨论】:

      【解决方案2】:

      你希望hit.Point 获得球体接触到它所击中的对撞机的位置。 Here are docs on raycasthit object

      【讨论】:

        猜你喜欢
        • 2021-12-19
        • 2021-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-27
        • 1970-01-01
        相关资源
        最近更新 更多