我正在使用这个:
要让我的角色在空中前进(使用喷气背包)。当我松开操纵杆时,我的角色会立即停下。我的想法是让角色在摇杆释放后继续向前移动,并逐渐放慢速度直至停止。看起来很傻,好像我的角色撞到了墙上一样。我应该使用什么力量/速度/变换,以便在我松开操纵杆后物理学可以使我的角色减速?
我的刚体设置如下:
质量:100 阻力:0 角阻力:0.05 使用重力 不是运动学的 插值:插值 碰撞检测:离散 冻结位置:无 冻结旋转:所有
角色还具有一个胶囊形碰撞器,不是触发器,也没有物理材料。
编辑:这是完整的代码,您可以查看是否存在代码冲突。
void Update(){
if(leftJoystick){
rb.AddRelativeForce(0, 0, 400f, ForceMode.Impulse);
}
}
要让我的角色在空中前进(使用喷气背包)。当我松开操纵杆时,我的角色会立即停下。我的想法是让角色在摇杆释放后继续向前移动,并逐渐放慢速度直至停止。看起来很傻,好像我的角色撞到了墙上一样。我应该使用什么力量/速度/变换,以便在我松开操纵杆后物理学可以使我的角色减速?
我的刚体设置如下:
质量:100 阻力:0 角阻力:0.05 使用重力 不是运动学的 插值:插值 碰撞检测:离散 冻结位置:无 冻结旋转:所有
角色还具有一个胶囊形碰撞器,不是触发器,也没有物理材料。
编辑:这是完整的代码,您可以查看是否存在代码冲突。
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;
}
}
transform.position.y >= MAX_HEIGHT
时。你能检查一下是否进入了那个 if 语句吗? - Serlite