简体   繁体   English

如何修复 Unity Rigidbody.AddForce 问题?

[英]How to fix Unity Rigidbody.AddForce problem?

I'm making a bounce pad (jump pad if you like it like that).我正在制作一个弹跳垫(如果你喜欢这样的弹跳垫)。 I wanted do make it bounce you OnTriggerEnter, which i have done using Rigidbody.AddForce.我想让它反弹你 OnTriggerEnter,这是我使用 Rigidbody.AddForce 完成的。

And my problem is that it once bounces you higher, once lower, and sometimes does not bounce at all, you just fall on it.我的问题是它曾经让你弹得更高,一次更低,有时根本不弹,你只是落在它上面。

Here's my code :这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BouncerBouncing : MonoBehaviour
{
    public float bounceForce;

    public Rigidbody playerRigidbody;

    void OnTriggerEnter(Collider collider)
    {
        if(collider.gameObject.tag == "Player")
        {
            playerRigidbody.AddForce(0, bounceForce, 0, ForceMode.Impulse);
        }
    }
}

Thanks for your help!谢谢你的帮助!

Applying an inpulse of force won't cancel any contrary motion.施加力脉冲不会取消任何相反的运动。 For example, if you're falling down and your Y velocity is -10, applying an impulse with a velocity of 10 upwards will give you a total of 0. If you want to replace your Y velocity, you can directly set the velocity instead of applying force.例如,如果你跌倒,你的 Y 速度是 -10,施加一个速度为 10 向上的冲量会给你总共 0。如果你想替换你的 Y 速度,你可以直接设置速度施加力。

playerRigidbody.velocity = new Vector3(playerRigidbody.velocity.x, bounceForce, playerRigidbody.velocity.z);

This line simply replaces the Y velocity with the jump force while keeping the X and Z values as they were.这条线简单地用跳跃力替换了 Y 速度,同时保持 X 和 Z 值不变。 This way, the bounce height will remain constant.这样,弹跳高度将保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM