- Home /
smooth transitions for Rigibodies objects on iPhone and iPad
Hi I have been trying to get smooth transitions for my objects with Rigibodies on the iPhone. I have tried making modification in the Time Manager and I got stuck. Right now everything seems to run choppy...
I also have a hero in my game and he runs choppy left and right too. Here's the movement part of the script:
private var moveDirection = Vector3.zero; function FixedUpdate() { MoveMent(); }
function MoveMent() { var controller: CharacterController = myTransform.GetComponent(CharacterController); moveDirection = Vector3.zero; fTravelDist = fTravelSpeed Time.deltaTime; if(sState == "MoveRight") { moveDirection = Vector3(fTravelDist,0,0); } else if(sState == "MoveLeft") { moveDirection = Vector3(fTravelDist-1,0,0);
} controller.Move(moveDirection); }
Thanks again for all the help. I am learning so much about unity and enjoying it the whole way along.
Answer by Peter G · Dec 25, 2010 at 01:10 PM
Character Controllers are not considered rigidbodies. I do not know if you have a rigid body on your object, but you should remove one if you have both.
Based on your movement script, the only problem I see is that you are calling your function from FixedUpdate() when you should be calling it from Update for 2 reasons:
Update gives better performance than fixedUpdate hence FixedUpdate should only be used if you are using a rigidbody.
You are multiplying your Speed by Time.deltaTime. Time.deltaTime is 1/FPS so you want a function that is called every frame such as Update(). Since Fixed Update is called a constant rate and Time.deltaTime varies, that is probably why you are getting stuttering. You could use Time.fixedDeltaTime if you insist on using FixedUpdate();
A few more random points to help your performance.
You should cache your CharacterController so that you do not have to find it every frame which is expensive. (see below)
You could inline your entire movement function. The compiler might do it anyway for you, but unnecessary function calls are not good. You might want to do some tests to see if there is any difference.
private var moveDirection = Vector3.zero; private var controller : CharacterController;
function Start () { controller = GetComponent(CharacterController); //Cache the controller }
function FixedUpdate() { MoveMent(); }
function MoveMent() {
moveDirection = Vector3.zero;
fTravelDist = fTravelSpeed * Time.deltaTime;
if(sState == "MoveRight")
{
moveDirection = Vector3(fTravelDist,0,0);
}
else if(sState == "MoveLeft")
{
moveDirection = Vector3(fTravelDist*-1,0,0);
}
controller.Move(moveDirection);
}
This has helped alot with speed thanks again. Question though. When you say inline the entire movement do you mean just no not have a function at all? Just put all the code in the Update?
Also Sorry for not being too clear with my Question. So I have figured out how to use the profiler to get the stats of my game.
cpu-player> $$anonymous$$: -160.4 max: 346.0 avg: 157.3 cpu-ogles-drv> $$anonymous$$: 0.9 max: 170.2 avg: 6.9 cpu-waits-gpu> $$anonymous$$: 0.1 max: 176.8 avg: 128.7 cpu-present> $$anonymous$$: 0.4 max: 166.7 avg: 11.1 frametime> $$anonymous$$: 14.9 max: 350.8 avg: 180.6 draw-call #> $$anonymous$$: 16 max: 18 avg: 17 | batched: 7 tris #> $$anonymous$$: 3212 max: 4185 avg: 3576 | batched: 1640 verts #> $$anonymous$$: 2240 max: 2744 avg: 2443 | batched: 836
player-detail> physx: 1.4 animation: 0.0 culling 0.0 skinning: 0.0 batching: 0.1 render: 1.7 fixed-update-count: 1 .. 5 mono-scripts> update: 3.1 fixedUpdate: 0.0 coroutines: 22.4 mono-memory> used heap: 290816 allocated heap: 356352 max number of collections: 0 collection total duration: 0.0
So I am not sure what I should be trying to get for my stats and I am sort of getting what I should do to improve them. So I guess what and how to fix my stats to get an ok frame rate is what I am after.
See this page. You appear to be taking the biggest hit from the GPU. Your CPU waited an average of 128.7 milleseconds for the GPU every frame. Also, your numbers are very far apart. $$anonymous$$ake sure you gave your game a few cycles to setup and s$$anonymous$$dy itself before you record any information. http://unity3d.com/support/documentation/$$anonymous$$anual/Optimizing%20Graphics%20Performance.html#iPhoneOptimizingGraphicsPerformance