C# 在突破赛中正确加速碰撞球
因此,我的资产中有这3个脚本(以及其他脚本)。我的总经理(GM)、我的球和我的疯子里克。当它碰到疯狂的砖块时,它应该每次都移动得快一点。在我的GM中,我使用GM实例将布尔快速球设置为false,并在crazybrick脚本中的碰撞时将其设置为true。然后在我的球脚本中,如果是真的,我会逐渐使球跑得更快,然后将fastball设置回false。我用调试日志对此进行了测试,每次速度都快了100f。在第一次测试中,一切似乎都很完美。但在随后的测试中,奇怪的事情发生了。球有时会跑得慢一些,或者在速度过快后从我的墙上射出,或者看起来根本没有变快。它总是不同的。下面是这三个脚本的相关代码C# 在突破赛中正确加速碰撞球,c#,unity3d,C#,Unity3d,因此,我的资产中有这3个脚本(以及其他脚本)。我的总经理(GM)、我的球和我的疯子里克。当它碰到疯狂的砖块时,它应该每次都移动得快一点。在我的GM中,我使用GM实例将布尔快速球设置为false,并在crazybrick脚本中的碰撞时将其设置为true。然后在我的球脚本中,如果是真的,我会逐渐使球跑得更快,然后将fastball设置回false。我用调试日志对此进行了测试,每次速度都快了100f。在第一次测试中,一切似乎都很完美。但在随后的测试中,奇怪的事情发生了。球有时会跑得慢一些,或者在速度过
public class GM : MonoBehaviour {
public bool fastBall = false;
public static GM instance = null;
}
public class CrazyBrick : MonoBehaviour {
public GameObject brickParticle;
void OnCollisionEnter ()
{
GM.instance.fastBall = true;
Instantiate (brickParticle, transform.position, Quaternion.identity);
GM.instance.DestroyBrick ();
Destroy (gameObject);
}
}
public class Ball : MonoBehaviour {
public float ballInitialVelocity = 600f;
public float newVelocity;
private Rigidbody rb;
private bool ballInPlay;
// Use this for initialization
void Awake () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void FixedUpdate () {
if (GM.instance.fastBall) {
newVelocity = newVelocity + 100f;
Debug.Log ("increment 100f: " + newVelocity);
rb.AddForce(new Vector3(newVelocity, newVelocity, 0));
GM.instance.fastBall = false;
}
if(Input.GetButtonDown("Fire1") && ballInPlay == false)
{
transform.parent = null;
ballInPlay = true;
rb.isKinematic = false;
rb.AddForce(new Vector3(ballInitialVelocity, ballInitialVelocity,
0));
}
}
}
公共类GM:单一行为{
公共足球快球=假;
公共静态GM实例=null;
}
公共课疯狂瑞克:单一行为{
公共游戏对象;
无效碰撞中心()
{
GM.instance.fastBall=true;
实例化(brickParticle、transform.position、Quaternion.identity);
GM.instance.DestroyBrick();
摧毁(游戏对象);
}
}
公共班级舞会:单一行为{
公共浮球初始速度=600f;
公共交通速度;
私人刚体;
私人布尔巴林普拉;
//用于初始化
无效唤醒(){
rb=GetComponent();
}
//每帧调用一次更新
无效固定更新(){
如果(GM.instance.fastBall){
新速度=新速度+100f;
Log(“增量100f:+newVelocity”);
rb.AddForce(新矢量3(新速度,新速度,0));
GM.instance.fastBall=false;
}
if(Input.GetButtonDown(“Fire1”)和&ballInPlay==false)
{
transform.parent=null;
ballInPlay=true;
rb.iskinetic=false;
rb.AddForce(新矢量3)(ballInitialVelocity,ballInitialVelocity,
0));
}
}
}
如果您知道要将刚体设置为的确切速度,则可以直接设置速度,而无需调用AddForce
。除此之外,您还调用了AddForce
,并将全速度加上100个单位。所以如果你的速度是50,那么你要计算150,并用它调用addforce,给你50+150,而不是仅仅给你150。如果要调用AddForce
,则应添加100
同样,如果你在X和Y方向上加上相等的力,你正在做的,那么这实际上比原来的量要多。如果你考虑毕达哥拉斯定理,直角三角形的斜边比其他两边长。所以,如果你把100加到X,100加到Y,实际上大约是每秒141个单位。您应该获得速度方向,并沿该方向添加100个单位,这避免了沿多个向量添加然后必须规范化该值的问题
在您正在使用的位置尝试类似的方法rb.AddForce()
:
也许这对你有用:
void FixedUpdate () {
if (GM.instance.fastBall) {
// Get the current velocity vector
var oldVelocity = rb.velocity;
// Get the current speed
var oldMagnitude = oldVelocity.magnitude;
// Get the current direction the rigid body is moving in
var direction = oldVelocity.normalized;
// The new velocity is in the same direction but faster than before
var newVelocity = direction * (oldMagnitude + 100f);
// Set the rigid body to the new velocity
rb.velocity = newVelocity;
GM.instance.fastBall = false;
}
if(Input.GetButtonDown("Fire1") && ballInPlay == false)
{
transform.parent = null;
ballInPlay = true;
rb.isKinematic = false;
// Get the current direction the rigid body is moving in
var direction = oldVelocity.normalized;
rb.velocity = direction * ballInitialVelocity;
}
}
好吧,我加上你的例子,我假设。大小和。归一化是向量3的一部分,但它不识别。奇怪的是,它们记录在这里:更具体地说,这里:这里:是的,我也看到了,但它在unity中以红色突出显示,并表示system.single不包含震级定义。您应该处理的是矢量3,而不是单个。速度应该返回一个向量3。我会将“var oldVelocity”更改为“Vector3 oldVelocity”,看看这是否会给您带来编译错误。另外,我可以验证“rb”确实是一个UnityEngine.RigidBody.ok,我最后标记了球并循环,在我的GM中使用了它,这很有效,谢谢
void FixedUpdate () {
if (GM.instance.fastBall) {
// Get the current velocity vector
var oldVelocity = rb.velocity;
// Get the current speed
var oldMagnitude = oldVelocity.magnitude;
// Get the current direction the rigid body is moving in
var direction = oldVelocity.normalized;
// The new velocity is in the same direction but faster than before
var newVelocity = direction * (oldMagnitude + 100f);
// Set the rigid body to the new velocity
rb.velocity = newVelocity;
GM.instance.fastBall = false;
}
if(Input.GetButtonDown("Fire1") && ballInPlay == false)
{
transform.parent = null;
ballInPlay = true;
rb.isKinematic = false;
// Get the current direction the rigid body is moving in
var direction = oldVelocity.normalized;
rb.velocity = direction * ballInitialVelocity;
}
}