本文介绍了移动时的Unity Ball摇晃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个球类物体,我正在使用明确的物理方程式弹跳.我尝试使用unity的物理引擎,但这还不够精确.我将球的位置设置为:

I have a ball game object that I am making bounce using explicit physics equations. I tried using unity's physics engine, but It was not exact enough. I am setting the balls position like so:

float dTime = Time.time - timeSinceBounce;
 ballPosition.x -= meterOffset / sensitivity;
 ballPosition.y = ballInitialPosition.y + GameConstants.Instance.GetBallYVel ()
 * dTime - (float)0.5 * GameConstants.Instance.GetGravity () * dTime * dTime; //Yi+Vy*t-0.5(G)(time^2)
 ballPosition.z = (-dTime * GameConstants.Instance.GetBallZVel ()
 + ballInitialPosition.z) * startConstant; //Zi + t*Vz
 ball.transform.position = ballPosition;

此代码在Update()方法中运行.当检测到碰撞时,时间和初始位置将重置,并且可以正常工作.目前,球可以正常弹跳,但是在快速移动时会摇动. FPS为80.我认为我的方程式不是错误的,因为它会正确地反弹,只是晃动.消除抖动的最佳方法是什么?谢谢!

This code runs in the Update() method. When a collision is detected the time and initial position as reset, and this works. Currently the ball bounces properly but shakes when it is moving quickly. The FPS is at 80. I don't think my equations are wrong because it bounces properly there is just a shake. What is the best way to remove the shake? Thank!

我决定编写自己的物理学,因为Unity的物理学引擎还有很多其他成分,例如摩擦和滚动,这些因素使球的弹跳变化.同样,相机的x和y是固定的,唯一改变的是相机的z位置. 在固定更新中效果更好

Edit 1: I decided to write my own physics because Unity's physics engine had a lot of other components like friction and roll that were making the ball's bounces vary. Also the camera's x and y is fixed the only thing that is changing is the camera's z position. It works a lot better in fixed update

推荐答案

应要求者的要求,我特此添加所有详细信息,即使没有看到实际的,完整的和完整的代码,也没有看到这种抖动"效果,难以给出确切和正确的答案.我会尽力而为.

所以让我们做到这一点.
1..晃动"的原因:很可能是您设置了某种动画,以及强制定位/旋转/等,并且这两个代码相互冲突.仔细检查您的 AnimatorController 动画师以及您的 Mecanim (如果您已经设置/配置了这些文件(也很好阅读:"Locomotion").
您还可以保留动画器窗口打开,以查看谁是为您执行摇晃"效果的棘手家伙,并进一步说明为什么会发生这种情况.
如果这些方法无济于事,请从头开始:最小化编写用于移动球,删除所有效果等的代码,并将它们逐个添加回去.迟早会出现摇晃"现象(如果使用最简洁的代码继续存在,请参见上文Animator/Locomotion问题)

As of asker's request I hereby add all the details I can, even though without seeing the actual, full and complete code along with seeing this "shake" effect it's challenging to give an exact and proper answer. I'll try and do my best though.

So let's do this.
1. "Shaking" reason: most probably you set up some kind of animation, along with a forced positioning/rotation/etc and these two codes work against each other. Double-check your AnimatorController and Animator as well as your Mecanim if you have set/configure these (also a good read: "Locomotion").
You can also keep the Animator Window open while you are testing from Unity UI, to see who is the tricky guy doing the "shaking" effect for you, and get one step closer to why it happens.
If these don't help, start from scratch: Minimize the code you wrote to move the ball, remove every effect, etc and add them back one by one. Sooner or later "shakieness" will come back (if it persist with the cleanest possible code, see above, Animator/Locomotion issue)

2.再考虑一遍,不要重写Unity Physics ...例如,您提到的
"... 物理引擎还有很多其他组成部分,例如摩擦和滚动球的弹跳会有所不同... .
现在,您可以关闭此功能.如果需要的话,完全可以.最简单的方法是在物理材料上将所有内容设置为零(即,摩擦力和弹性)但是如果您想让Unity忽略所有的实际物理学"并让您使用GameObject做您想做的事,请在isKinematic 属性.有趣的部分是,您仍然可以在任何地方和任何地方发生冲突,可以 AddForce 等,但重力,摩擦或任何物理"都不会影响您.
如果您对Unity不满意(看来不是),我强烈建议您先对其进行一点了解,因为这是非常重要的好的又非常强大的引擎-因此,不要放弃它的功能,因为它们不会像您最初预期的那样起作用.例如,几周前塞巴斯蒂安·拉格(Sebastian Lague)开始了一个很棒的初学者系列.这个人是一位老师,他的视频简短,有趣且信息丰富,因此我可以推荐他的频道.

2. Think again not rewriting Unity Physics... You mention for example
"...physics engine had a lot of other components like friction and roll that were making the ball's bounces vary...".
Now, this is something you can turn off. Completely if you want. Easiest way is to set everything to zero on Physics Material (i.e. friction and bouncieness) but if you want Unity to ignore all and every "actual physics" and let you do what you want with your GameObject, tick in the isKinematic property of the Rigidbody. The fun part of it, you'll still collide if and where you want, can AddForce, etc yet neither gravity nor friction or "anything physical" affect you.
If you are not comfy with Unity (and you are not it seems), I highly recommend learning about it a bit first as it is a very good and very powerful engine - so don't throw it's features away as they don't work as you initially expected they'll do. For example, Sebastian Lague just started a great beginner series a few weeks ago. The guy is a teacher, his videos are both short, fun and full of information so I can just recommend his channel.

3..从您的代码开始,我将进行以下更改:

3. As of your code I would make these changes:

public static class GameConstants { //no monobehaviour == can be a "proper, global" singleton!
   public static float GetGravity {
      get { return [whatever you want]; }
      //why not Physics.gravity, using locally when and where it's needed?
   }
   //etc, add your methods, properties and such
   //use this approach if and only IF you access these values application wide!
   //otherwise, instead of a """singleton""" logic you just implemented,
   //consider adding an empty GameObject to the Scene, name it "GM" and
   //attach a "GameManager" script to it, having all the methods and
   //properties you'll need Scene-wide! You can also consider
   //using PlayerPrefs (see help link below) instead of a global static
   //class, so you save memory (but will need to "reach out" for the data)
}
private void FixedUpdate() { //FIXED update as you'll work with physics! //float dTime = Time.time - timeSinceBounce; //not needed //positioning like this is not recommended, use AddForce or set direction and velocity instead ballPosition.x -= meterOffset / sensitivity; ballPosition.y = ballInitialPosition.y + GameConstants.Instance.GetBallYVel () * Time.fixedDeltaTime - 0.5f * GameConstants.Instance.GetGravity () * Time.fixedDeltaTime * Time.fixedDeltaTime; //Yi+Vy*t-0.5(G)(time^2) ballPosition.z = (-dTime * GameConstants.Instance.GetBallZVel () + ballInitialPosition.z) * startConstant; //Zi + t*Vz ball.transform.position = ballPosition; }

说实话,

我只是扔掉那些代码,实现弹跳球 Unity教程提供的示例,对其进行更改(请参阅第2点),然后从那里继续前进.

I would just throw that code away, implement the Bouncing Ball example Unity Tutorials offer, alter it (see point #2) and work my way up from there.

项目未链接,但我在代码中提到: PlayerPrefs Physics.gravity 刚性体.velocity .
注意,速度"忽略了物理部分(加速,拖拉"等),即物体突然开始以您设置的速度运动(这就是Unity5建议使用AddForce而不是上面链接的原因- )

Items not linked but I mentioned in code:PlayerPrefs, Physics.gravity and Rigidbody.velocity.
Note that 'velocity' is ignoring parts of physics (speeding up, being "dragged", etc) i.e. objects suddenly start moving with the speed you set (This is why Unity5 recommends AddForce instead -linked above-)

您也可以通过时间管理更改FixedUpdate()频率 a>,但我不确定这是否是必需的(即仅用于弹跳球)

It might also come handy you can alter the FixedUpdate() frequency via Time Management, but I'm not sure this is yet needed (i.e. just for bouncing a ball)

我希望这会有所帮助,并且该球将开始弹跳,跳跃和跳舞,以及您想要它做的所有事情;)
干杯!

I hope this helps and that ball is going to bounce and jump and dance and everything you want it to do ;)
Cheers!

这篇关于移动时的Unity Ball摇晃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:38