【问题标题】:auto aim for grappling gun unity自动瞄准抓枪团结
【发布时间】:2021-08-22 11:51:30
【问题描述】:

我想为我的抓钩添加辅助,因为它很难瞄准并正确定位 我希望它捕捉到最近的可抓物体(比如在 karlson) 控制器是基于刚体的 抓钩代码:

public class GrapplingGun : MonoBehaviour {

private LineRenderer lr;
private Vector3 grapplePoint;
public LayerMask whatIsGrappleable;
public Transform gunTip, camera, player;
private float maxDistance = 100f;
private SpringJoint joint;

void Awake() {
    lr = GetComponent<LineRenderer>();
}

void Update() {
    if (Input.GetMouseButtonDown(0)) {
        StartGrapple();
    }
    else if (Input.GetMouseButtonUp(0)) {
        StopGrapple();
    }
}

//Called after Update
void LateUpdate() {
    DrawRope();
}

/// <summary>
/// Call whenever we want to start a grapple
/// </summary>
void StartGrapple() {
    RaycastHit hit;
    if (Physics.Raycast(camera.position, camera.forward, out hit, maxDistance, whatIsGrappleable)) {
        grapplePoint = hit.point;
        joint = player.gameObject.AddComponent<SpringJoint>();
        joint.autoConfigureConnectedAnchor = false;
        joint.connectedAnchor = grapplePoint;

        float distanceFromPoint = Vector3.Distance(player.position, grapplePoint);

        //The distance grapple will try to keep from grapple point. 
        joint.maxDistance = distanceFromPoint * 0.8f;
        joint.minDistance = distanceFromPoint * 0.25f;

        //Adjust these values to fit your game.
        joint.spring = 4.5f;
        joint.damper = 7f;
        joint.massScale = 4.5f;

        lr.positionCount = 2;
        currentGrapplePosition = gunTip.position;
    }
}


/// <summary>
/// Call whenever we want to stop a grapple
/// </summary>
void StopGrapple() {
    lr.positionCount = 0;
    Destroy(joint);
}

private Vector3 currentGrapplePosition;

void DrawRope() {
    //If not grappling, don't draw rope
    if (!joint) return;

    currentGrapplePosition = Vector3.Lerp(currentGrapplePosition, grapplePoint, Time.deltaTime * 8f);
    
    lr.SetPosition(0, gunTip.position);
    lr.SetPosition(1, currentGrapplePosition);
}

public bool IsGrappling() {
    return joint != null;
}

public Vector3 GetGrapplePoint() {
    return grapplePoint;
}

} Lorem ipsum dolor sit amet, consectetur adipiscing elit。 Quisque iaculis、nulla eget accumsan iaculis、tellus arcu eleifend risus、eu convallis turpis sem vitae augue。

【问题讨论】:

  • 你有 hit.point 这是一组坐标。您只需要检查所有可抓取对象并找到最接近hit.point 的对象,然后将grapplePoint 更改为您找到的可抓取对象的位置。为此,您需要维护一个可以检查的可抓取对象列表。根据场景中有多少可抓取对象,您可能需要动态过滤/更新该列表,以便它只有一组可管理的可抓取对象。
  • @HumanWrites,它不会瞄准游戏对象的中间吗?
  • 是的,但是您可以通过将游戏对象放置在您希望抓钩能够附着的位置并找到其中之一的位置来解决此问题。

标签: c# unity3d


【解决方案1】:

如果我可以问,您如何列出清单?

对不起,如果我写的地方不对,那是因为我对这件事不熟悉。 =-)

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 2018-04-01
相关资源
最近更新 更多