【发布时间】:2021-11-03 21:07:45
【问题描述】:
我正在尝试检测刚体中的 y 速度,以确定玩家是否可以跳跃。由于某种原因,我从 rb.velocity[2] 获得的组件与我在控制台中记录 rb.velocity 时看到的不匹配。一些帮助理解如何从刚体获得垂直速度分量将非常有帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public Rigidbody rb;
public Vector3 xvector = new Vector3 (1, 0, 0);
public Vector3 yvector = new Vector3 (0, 1, 0);
public float strafeforce = 1f ;
public float jumpforce = 15f;
// Update is called once per frame
void Update()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (Input.GetKey("right") )
{
rb.AddForce(strafeforce, 0, 0, ForceMode.Impulse) ;
}
if (Input.GetKey("left") )
{
rb.AddForce(-strafeforce, 0, 0, ForceMode.Impulse) ;
}
if (Input.GetKey("up") )
{
rb = GetComponent<Rigidbody>();
if (rb.velocity[2] == 0)
{
rb.AddForce(0, jumpforce, 0, ForceMode.Impulse) ;
}
Debug.Log(rb.velocity);
Debug.Log(rb.velocity[2]);
}
}
}
所以问题在于,由于某种原因,来自 rb.velocity 的第二个值与我从 rb.velocity[2] 获得的值不匹配。
【问题讨论】:
-
您是否尝试过改用
rb.velocity.x、rb.velocity.y和rb.velocity.z?我相信这是获得刚体速度分量的推荐方法。
标签: c# unity3d rigid-bodies