物体在空中时移动方向不正确 [英] Incorrect moving direction when object in midair

查看:119
本文介绍了物体在空中时移动方向不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扩展我先前的问题,我的角色(临时)是一个立方体.多维数据集使用C#脚本和刚体组件进行映射.

Extending my previous question, my character is (temporally) a cube. The cube is mapped with a C# script and Rigid Body component.

我使用以下代码来更改角色(刚体)的方向:

I use the following code to change my character (which is a rigid body)'s direction:

public float speed = 0;
public Vector3 jumpHeight = new Vector3();
public bool isOnGround = true

void Update () {
    bool isTouched = false;
    if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began) {
        isTouched = true;
    }
    if ((Input.GetButtonDown("Jump") || isTouched) && isOnGround) {
        isOnGround = false; 
        rigidbody.AddForce(jumpHeight, ForceMode.VelocityChange);
    }
    if (Input.GetKeyDown(KeyCode.LeftArrow)) {
        transform.Rotate(0, -90, 0);
        rigidbody.velocity = Vector3.zero;
        rigidbody.angularVelocity = Vector3.zero;
    }
    if (Input.GetKeyDown(KeyCode.RightArrow)) {
        transform.Rotate(0, 90, 0);
        rigidbody.velocity = Vector3.zero;
        rigidbody.angularVelocity = Vector3.zero;
    }
}

void FixedUpdate() {
    if (isOnGround) {
        rigidbody.AddForce(transform.forward * speed, ForceMode.Acceleration);
    }
}

其中isOnGround是一个布尔值,当Runner对象接触地面时将其设置为true.但是,在离开地面之前,它会沿Z轴移动.当设备离开地面时,它会滑动到X-Z轴.我想念什么?

where isOnGround is a boolean, which sets to true when the Runner object touches the ground. However, before it leaves the ground, it moves along Z axis. When the unit leaves the ground, it slides to X-Z axis. What did I miss?

注意:假设Runner对象的质量为3.

Note: Given that the Runner object has mass of 3.

推荐答案

如果jumpHeight设置为Vector3(0,7,0),则问题消失了,并且Runner对象与轴对齐跳跃.

If the jumpHeight is set to Vector3(0,7,0), the problem is gone and the Runner object is jumping aligned with axes.

这篇关于物体在空中时移动方向不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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