C# 延迟在对象中附加重力

C# 延迟在对象中附加重力,c#,unity3d,game-physics,game-development,C#,Unity3d,Game Physics,Game Development,我有一个可以从左到右移动的球,它还有一个附加的重力,会增加超时时间。 问题:如何延迟将重力附着/添加到对象上大约3秒钟。 期望:在开始时,我仍然可以在没有重力的情况下将球从左向右移动,因此在y轴上的位置相同,但3秒钟后,球将有重力并开始下落 public class PlayerController : MonoBehaviour { //Init public Vector2 speedMinMax; public Vector2 gravityMinMax;

我有一个可以从左到右移动的球,它还有一个附加的重力,会增加超时时间。
问题:如何延迟将重力附着/添加到对象上大约3秒钟。
期望:在开始时,我仍然可以在没有重力的情况下将球从左向右移动,因此在y轴上的位置相同,但3秒钟后,球将有重力并开始下落

public class PlayerController : MonoBehaviour
{
    //Init
    public Vector2 speedMinMax;
    public Vector2 gravityMinMax;
    float speed;
    float move;
    float gravity;
    float halfPlayerWidth;
    private float screenHalfWidthInWorldUnit;
    //Components
    Rigidbody2D rigidbody2D;

    void Awake()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    public void Start()
    {
        SpawnPlayer();
        halfPlayerWidth = transform.localScale.x / 2f;
        screenHalfWidthInWorldUnit = Camera.main.aspect * Camera.main.orthographicSize + halfPlayerWidth;
    }

    void Update()
    {
        IncreaseGravity();
        PlayerBounds();
    }

    void FixedUpdate()
    {
        PlayerInput();
    }

    void SpawnPlayer()
    {
        transform.position = new Vector2(0, Camera.main.orthographicSize - transform.localScale.y - 2f);
    }

    void PlayerInput()
    {
        //PlayerInput
        //Keyboard
        speed = Mathf.Lerp(speedMinMax.x, speedMinMax.y, Difficulty.GetDifficultyPercent());
        move = Input.GetAxis("Horizontal");
        rigidbody2D.velocity = new Vector2(move * speed, rigidbody2D.velocity.y);
    }
    
    void IncreaseGravity()
    {
        gravity = Mathf.Lerp(gravityMinMax.x, gravityMinMax.y, Difficulty.GetDifficultyPercent());
        rigidbody2D.gravityScale = gravity;
    }
}
公共类PlayerController:MonoBehavior
{
//初始化
公共向量2 speedMinMax;
公共向量2引力矩阵;
浮动速度;
浮动运动;
漂浮重力;
浮球半边旗;
私有浮动屏幕半宽度单位;
//组成部分
刚体2d刚体2d;
无效唤醒()
{
rigidbody2D=GetComponent();
}
公开作废开始()
{
SpawnPlayer();
halfPlayerWidth=transform.localScale.x/2f;
screenHalfWidthInWorldUnit=Camera.main.aspect*Camera.main.orthographicSize+halfPlayerWidth;
}
无效更新()
{
增加重力();
PlayerBounds();
}
void FixedUpdate()
{
PlayerInput();
}
无效玩家()
{
transform.position=newvector2(0,Camera.main.orthographicSize-transform.localScale.y-2f);
}
void PlayerInput()
{
//播放机输入
//键盘
speed=Mathf.Lerp(speedMinMax.x,speedMinMax.y,难点.GetDifficultyPercent());
move=Input.GetAxis(“水平”);
rigidbody2D.velocity=新矢量2(移动*速度,rigidbody2D.velocity.y);
}
void递增重力()
{
gravity=Mathf.Lerp(gravityMinMax.x,gravityMinMax.y,难度.gethardicultypercent());
刚体2d.gravityScale=重力;
}
}
组件控制附着对象的重力。您可以创建一个协同程序,并将gravityScale设置为0以禁用。3秒钟后,将其设置为旧值或所需值。 `

在启动函数时触发此协同程序

void Start()
{
    StartCoroutine(WaitForGravity());
}
void Start()
{
    StartCoroutine(WaitForGravity());
}