如何在FixedUpdate中移动刚体,但我的角色仍然受到重力影响? [英] How do I move the Rigidbody in FixedUpdate, but still have my character be affected by gravity?

查看:263
本文介绍了如何在FixedUpdate中移动刚体,但我的角色仍然受到重力影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以

在下面,您会看到我最初用来控制角色移动的内容.

Below you see what I initially used to control my characters movement.

_RigidBody.velocity = _ConstantWalkingSpeed * direction.normalized;

这适用于4个方向走动.但是,如果我的角色跌倒了,他会慢慢掉下veeeeeery.然后,如果我在半空中禁用步行脚本,重力就会上升并以正常速度运行.

This works for walking around in 4 directions. However, if my character falls over an edge, he falls veeeeeery slowly. If I then in mid air disable the walking script, gravity picks up and goes at normal speed.

这使我相信我的运动脚本会以某种方式影响重力效应.

This leads me to believe that my movement script somehow affects the gravity effect.

因此,我尝试了以下解决方案:

As such I tried this solution:

_RigidBody.velocity = _ConstantWalkingSpeed * direction.normalized + new Vector3(0f, _RigidBody.velocity.y, 0f);

那也不起作用,所以我尝试了这个:

That didn't work either, so I tried this:

_RigidBody.velocity = _ConstantWalkingSpeed * direction.normalized + new Vector3(0f, _RigidBody.velocity.y, 0f) + Physics.gravity;

那确实使引力起作用了,但是引力变得如此之强,以至于我无法动弹.

That did make the gravity work, but then the gravity became so strong that I can't move.

我只尝试添加Physics.gravity并跳过了新的矢量部分,但是重力仍然太强.

I tried only adding Physics.gravity and skipping the new vector part, but then the gravity is still too strong.

TL; DR

我使用的移动脚本会影响玩家的向下重力,而这不应该.我希望他四处走动,但仍然受到重力的影响.我尝试过的想法行不通.

The movement script I use affects the players downward gravity, which it shouldn't. I want him to move around but still be affected by gravity. Ideas I have tried didn't work.

请注意,我宁愿将重力保持在-9.81.

Please note that I'd prefer to keep the gravity at -9.81.

希望你们有一个可行的建议:-)

Hoping you guys have a suggestion that works :-)

推荐答案

您可以使用RigidBody.movePositon:

这是我写的一个快速示例脚本:

Here is a quick example script I wrote up:

using UnityEngine;

public class simpleMoveRB : MonoBehaviour {
    public Rigidbody myBody;
    public float _constantWalkSpeed = 3.0f;

    Vector3 moveDirection = Vector3.zero;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update() {
        if (Input.GetKey(KeyCode.A))
        {
            moveDirection.x = -1.0f;
        }
        else if(Input.GetKey(KeyCode.D))
        {
            moveDirection.x = 1.0f;
        }
        else
        {
            moveDirection.x = 0.0f;
        }

        if(Input.GetKeyDown(KeyCode.Space))
        {
            myBody.AddForce(Vector3.up * 500.0f);
        }
    }

    private void FixedUpdate()
    {
            myBody.MovePosition(transform.position + moveDirection * _constantWalkSpeed * Time.deltaTime);
    }
}

您可以使用此脚本处理运动,并且仍然在对象上进行重力工作.

You can handle movement using this script, and still have gravity work on your object.

这篇关于如何在FixedUpdate中移动刚体,但我的角色仍然受到重力影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆