gpt4 book ai didi

c# - 统一 5 : How to slow down rigidbody naturally instead of sudden stop

转载 作者:行者123 更新时间:2023-12-04 02:09:12 26 4
gpt4 key购买 nike

我正在使用这个:

void Update(){
if(leftJoystick){
rb.AddRelativeForce(0, 0, 400f, ForceMode.Impulse);
}
}

让我的角色在空中向前移动(使用喷气背包)。当我松开操纵杆时,我的角色立即停止。我想要的是角色在释放操纵杆后继续前进,并随着时间的推移逐渐减速并逐渐停止。我的角色像撞到墙上一样停下来,这看起来很愚蠢。我应该使用什么样的力/速度/变换,以便在我松开操纵杆后物理减慢我的角色?

我的刚体设置:

  • 质量:100
  • 拖动:0
  • 角度阻力:0.05
  • 利用重力
  • 不是运动学的
  • 插值:插值
  • 碰撞检测:离散
  • 卡住位置:无
  • 卡住旋转:全部

角色还有一个胶囊碰撞器,它不是触发器,也没有物理 Material 。

编辑:这是完整的代码,因此您可以查看我的代码中是否存在任何冲突

using UnityEngine;
using System.Collections;
using UnityStandardAssets.CrossPlatformInput;

public class JetpackController : MonoBehaviour{

Rigidbody rb;

// controller axes
[SerializeField]
public string speedAxis = "Vertical";
public string rotateAxis = "HorizontalRotate";

[SerializeField]
public float forwardSpeed;
public float upSpeed;
public float rotateSpeed;
public float rotationIgnoreZone;
public float MAX_HEIGHT;

float speedInput;
float rotateInput;
public bool leftJoystick;
public bool rightJoystick;

ParticleSystem jetpackFire1;

Vector3 originalGravity;

// access Joystick script in Standard assets to detect when right joystick is held down
Joystick joystick_left;
Joystick joystick_right;

void Start () {
if (GetComponent<Rigidbody> ()) {
rb = GetComponent<Rigidbody> ();
} else {
Debug.LogError ("Player needs to have a rigidbody.");
}

// get Joysticks
if (GameObject.Find ("MobileJoystick_left").GetComponent<Joystick> ()) {
joystick_left = GameObject.Find ("MobileJoystick_left").GetComponent<Joystick> ();
} else {
Debug.LogError ("Either gameobject with name 'MobileJoystick_right' does not exist in the scene or it does not have script called Joystick.cs attached to it.");
}
if (GameObject.Find ("MobileJoystick_right").GetComponent<Joystick> ()) {
joystick_right = GameObject.Find ("MobileJoystick_right").GetComponent<Joystick> ();
} else {
Debug.LogError ("Either gameobject with name 'MobileJoystick_right' does not exist in the scene or it does not have script called Joystick.cs attached to it.");
}

jetpackFire1 = GameObject.Find("JetpackFire1").GetComponent<ParticleSystem> ();
jetpackFire1.playbackSpeed = 5.0f;

originalGravity = Physics.gravity;
}

void FixedUpdate () {

GetInput ();

// move forward and backward according to left joysticks input
if(leftJoystick){
// left joystick held down
rb.AddRelativeForce(0, 0, forwardSpeed*speedInput, ForceMode.Impulse);
}

// if right joystick is used
if (rightJoystick) {
jetpackFire1.Play ();

if (transform.position.y < MAX_HEIGHT) {
// allow going up
//rb.AddRelativeForce (0, upSpeed, 0, ForceMode.Impulse);
rb.AddForce(transform.forward * forwardSpeed);
} else if (transform.position.y >= MAX_HEIGHT) {
// prevent player to go any further up and also falling
Physics.gravity = Vector3.zero; // no gravity to prevent player from falling
rb.velocity = Vector3.zero;
}
// rotate
// always keep rotating (player can go past look at joystick dir)
if (rotateInput >= rotationIgnoreZone || rotateInput <= -rotationIgnoreZone) {
transform.Rotate(Vector3.up * rotateInput, Time.deltaTime * rotateSpeed);
}
} else {
jetpackFire1.Stop ();
// if right joystick is released
Physics.gravity = originalGravity; // normal gravity -> player can fall
}

}

public void GetInput(){
speedInput = CrossPlatformInputManager.GetAxis (speedAxis);
rotateInput = CrossPlatformInputManager.GetAxis (rotateAxis);
// access Joystick.cs script to grab wether right joystick is pressed or not
leftJoystick = joystick_left.leftJoystickPressed;
rightJoystick = joystick_right.rightJoystickPressed;
}

最佳答案

还不能评论

正如 J.Small 所说,您可以将速度乘以一个大约 0.95 的数字来慢慢减慢刚体的速度。如果您将乘法放在 Update 方法中,我建议您将 ~0.95 数字与 Time.deltaTime 相乘,所以速度的降低将与耗时成正比。

void Update(){
if(leftJoystick){
rb.AddRelativeForce(0, 0, 400f, ForceMode.Impulse);
}

if(noJoystick){
rb.velocity = rb.velocity * 0.95 * Time.deltaTime;
//Or: rb.velocity.x
}
}

如果你想以更可配置的方式减慢刚体的速度,你也可以施加少量的力而不是乘以速度(如果你将它放入更新中,该量应该基于 Time.deltaTime - 它被称为一次一帧),但上面的方法更简单。

关于c# - 统一 5 : How to slow down rigidbody naturally instead of sudden stop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40349127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com