sphere cast 仅输出游戏对象的中心

sphere cast outputting only the center of the game object

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

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

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

Physics.SphereCast 完全没有输出。

是你用的hit.transform.position当然是命中对象的一般枢轴点

SphereCast为您提供RaycastHit with a lot of additional information like in particular the 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;
    }
}