当前位置: 首页 > 知识库问答 >
问题:

团结拼搏更像泰山

祁乐邦
2023-03-14

我正在努力使抓钩更流畅,但目前为止,它非常颠簸,没有正确的感觉。它当前会排成一条线,并将玩家拉到那里。我还没有尝试任何东西,因为我甚至不确定我们是否要开始修复这个问题。下面是所有抓斗代码`使用系统。收藏;使用System.Collections。通用的使用UnityEngine;

[需要组件(类型(SFPSC_PlayerMovement))] // 玩家运动也需要刚体公共类SFPSC_GrapplingHook: 单行为 { 公共布尔正在抓取 { 获取 { 返回是抓取; } } }

private SFPSC_PlayerMovement pm;
private Rigidbody rb;
private int segments;
private void Start()
{
    segments = rope.segments;
    pm = this.GetComponent<SFPSC_PlayerMovement>();
    rb = this.GetComponent<Rigidbody>();
}

private bool isGrappling = false;
private void Update()
{
    if (crossHairSpinningPart != null)
    {
        // we need 2 raycasts bc w/ 1 you can grapple through colliders which isn't good
        if (Physics.Raycast(SFPSC_FPSCamera.cam.transform.position, SFPSC_FPSCamera.cam.transform.forward, out hitInfo, maxGrappleDistance, layerMask))
        {
            hitName = hitInfo.collider.name;
            if (Physics.Raycast(SFPSC_FPSCamera.cam.transform.position, SFPSC_FPSCamera.cam.transform.forward, out hitInfo, maxGrappleDistance))
            {
                if (hitName != hitInfo.collider.name)
                    goto _else;
                crossHairSpinningPart.gameObject.SetActive(true);
                crossHairSpinningPart.Rotate(Vector3.forward * crossHairSpinSpeed * Time.deltaTime);
                goto _out;
            }
        }

        _else:
        crossHairSpinningPart.gameObject.SetActive(false);
    }
    _out:

    if (!isGrappling)
    {
        if (Input.GetKeyDown(SFPSC_KeyManager.Grapple))
            Grapple();
        
        return;
    }
    else
    {
        if (!Input.GetKey(SFPSC_KeyManager.Grapple))
            UnGrapple();
        GrappleUpdate();

        return;
    }
}

[Header("Properties")]
public float maxGrappleDistance = 100.0f;
public SFPSC_Rope rope;
public float maximumSpeed = 100.0f;
public float deceleration = 2500.0f; // This is how much the player is going to decelerate after stopped grappling
public float deceleratingTime = 1.4f; // This is the time the decelerating is going to act on the player after stopped grappling
public RectTransform crossHairSpinningPart;
public float crossHairSpinSpeed = 200.0f;
public float distanceToStop = 2.0f;
public LayerMask layerMask;
public float grappleCooldown = 1.0f;
private bool isBlocked = false;

private Transform location; // the grappled location
private RaycastHit hitInfo;
private string hitName;
public void Grapple()
{
    if (isBlocked)
        return;

    // we need 2 raycasts bc w/ 1 you can grapple through colliders which isn't good
    if (Physics.Raycast(SFPSC_FPSCamera.cam.transform.position, SFPSC_FPSCamera.cam.transform.forward, out hitInfo, maxGrappleDistance, layerMask))
    {
        hitName = hitInfo.collider.name;
        if (Physics.Raycast(SFPSC_FPSCamera.cam.transform.position, SFPSC_FPSCamera.cam.transform.forward, out hitInfo, maxGrappleDistance))
        {
            if (hitName != hitInfo.collider.name)
                return;
            // We create a GameObject and we parent it to the grappled object. 
            // If we don't parent it to the object and the object moves the player is stuck only on one location instead of the moving object.
            location = new GameObject().transform;//Instantiate(new GameObject(), hitInfo.point, Quaternion.identity).transform;
            location.position = hitInfo.point;
            location.parent = hitInfo.collider.transform;

            if (decelerateTimer != 0.0f)
                StopCoroutine(Decelerate());
            pm.DisableMovement();
            // Rope attaching
            rope.segments = (int)((hitInfo.distance / maxGrappleDistance) * segments);
            rope.Grapple(transform.position, hitInfo.point);

            rb.useGravity = false;
            isGrappling = true;
        }
    }
}

private Vector3 grappleForce;
public void UnGrapple()
{
    if (!isGrappling)
        return;
    if (location != null)
        Destroy(location.gameObject);
    if (decelerateTimer == 0.0f)
        StartCoroutine(Decelerate());
    else
        decelerateTimer = 0.0f;

    pm.EnableMovement();
    // Rope detaching
    rope.UnGrapple();

    Invoke("UnblockGrapple", grappleCooldown);
    
    rb.useGravity = true;
    isGrappling = false;
}

private void UnblockGrapple()
{
    isBlocked = false;
}

private float decelerateTimer = 0.0f, max;
private IEnumerator Decelerate()
{
    WaitForEndOfFrame wfeof = new WaitForEndOfFrame();
    max = deceleratingTime * Mathf.Clamp01(targetDistance / 10.0f) * Mathf.Clamp01(rb.velocity.magnitude / 30.0f);
    for (; decelerateTimer < max; decelerateTimer += Time.deltaTime)
    {
        rb.AddForce(-rb.velocity.normalized * deceleration * (1.0f - decelerateTimer / max) * Mathf.Clamp01(rb.velocity.sqrMagnitude / 400.0f) * Time.deltaTime, ForceMode.Acceleration);
        yield return wfeof;
    }
    decelerateTimer = 0.0f;
}

private Vector3 dir;

private float speed = 0.0f, targetDistance;
private void GrappleUpdate()
{
    if (location == null)
        return;
    
    targetDistance = Vector3.Distance(transform.position, location.position);
    rope.segments = (int)((targetDistance / maxGrappleDistance) * segments);
    dir = (location.position - transform.position).normalized;
    
    rb.velocity = Vector3.Lerp(rb.velocity, dir * maximumSpeed * Mathf.Clamp01(targetDistance / (4.0f * distanceToStop)), Time.deltaTime);

    // Rope updating
    rope.UpdateStart(transform.position);
    rope.UpdateGrapple();
}

private Vector3 ClampMag(Vector3 vec, float maxMag)
{
    if (vec.sqrMagnitude > maxMag * maxMag)
        vec = vec.normalized * maxMag;
    return vec;
}

} `

共有1个答案

姚俊材
2023-03-14

尝试使用FixedUpdate代替Update来进行基于物理的工作(基本上现在更新中的所有代码)。更新取决于计算机的时钟速度和刷新率(或多或少),并以相当不规则的间隔被调用,因为在当前帧完成处理后,下一帧将调用下一个更新FixedUpdate使其与帧速率无关。

此外,您可以使用< code > application . target frame rate 来限制您的帧速率,并将其限制为一个不错的FPS。

您也可以将您的运动与Time.deltaTime相乘以获得更平滑的运动,尽管这是一种标准做法,但作为平滑值使用仍有争议。

 类似资料:
  • 我正在努力使用Jolt进行转换。 输入: 谢谢

  • 我运行了这段代码。结果似乎没有生成result.png: 并且误差为

  •        LocaSpaceViewer可对tif格式、grd格式和img格式的数据进行拼接,在弹出的对话框中点击添加数据,可以添加单个的影像数据,点击添加目录,可以同时添加整个文件夹的多个影像数据,这里我们选择单个添加数据,依次将已经下载好的数据(tif格式)添加进来。        点击删除数据或删除所有,可以选择对话框中已添加的数据或全部数据进行删除。        添加完成后设置好输出

  • 影像拼接        LocaSpaceViewer可对tif格式、grd格式和img格式的数据进行拼接,在弹出的对话框中点击添加数据,可以添加单个的影像数据,点击添加目录,可以同时添加整个文件夹的多个影像数据,这里我们选择单个添加数据,依次将已经下载好的数据(tif格式)添加进来。        点击删除数据或删除所有,可以选择对话框中已添加的数据或全部数据进行删除。        添加完成后

  • 我一直在运行一个从多个相机拼接图像的项目,但我认为我遇到了瓶颈......我对这个问题有一些问题。 我想尝试在未来将它们安装在车辆上,这意味着相机的相对位置和方向是固定的。 此外,由于我使用多个摄像机,并尝试使用单应性从中缝合图像,我将尽可能靠近摄像机,以减少误差(由于摄像机的焦点不在同一位置,并且摄像机占据一定空间是不可能的)。 这是我的实验短片http://www.youtube.com/wa

  • 这是代码 这里还有一些图片的链接:https://www.dropbox.com/sh/ovzkqomxvzw8rww/AAB2DDCrCF6NlCFre7V1Gb6La?dl=0非常感谢你,拉菲