【发布时间】:2014-02-13 21:48:26
【问题描述】:
所以我设法修复了我的蹦床代码,让玩家可以像在蹦床上一样弹起。但是,我在使用此代码时遇到了 2 个问题。
首先,当在蹦床上弹跳时,它基本上会将玩家直接推入平流层,他们只是继续前进,永不停息,就好像没有重力将他们击落一样。即使我在播放器刚体设置中检查了重力。
其次,即使玩家不在蹦床附近,例如在没有附加蹦床脚本代码的平台上,将导致玩家在游戏开始时立即被抛到空中。
代码如下:
using UnityEngine;
using System.Collections;
public class small_trampoline_bounce : MonoBehaviour
{
bool willBounce = false;
float bounceHeight = 10;
public Transform Player;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 velocity = Player.rigidbody.velocity;
if (willBounce)
{
Player.rigidbody.velocity = new Vector3(velocity.x, 0, velocity.z);
Player.rigidbody.AddForce(0, bounceHeight, 0, ForceMode.Impulse);
willBounce = false;
}
}
void OnCollisionEnter(Collision Trampoline)
{
if (Trampoline.gameObject.name == "Player")
{
willBounce = true;
}
}
}
这也是蹦床和播放器的当前设置状态
播放器设置: http://gyazo.com/b4d924849a86e5158361f6081948e39f.png
【问题讨论】:
-
你的重力设置为多少(类似于编辑 --> 设置 --> 物理)
-
我看到你检查了播放器上的“使用重力”选项,但你是否也在某处设置了
Physics.gravity? -
@BlueRaja-DannyPflughoeft 我的重力设置如下:gyazo.com/4676fd1f6e8d4984ee371cb70dac1564.png
-
@StevenHansen 你看到的代码是我目前唯一的代码。对于我的重力设置,请参阅我刚刚为 BlueRaja 发布的链接