Hi, i´m working on a 2D game using Unity Iphone and i have a really big problem. What should i use? Character Controller? Rigidbody?
My character should do:
- Cllic on screen (Character moves to this point on x axis)
- Gesture to jump (Jump with the direction and force given by gesture)
- Fall of and die if fall is enought big
Now i´m using a rigidbody, a box collider and a plane with the sprite. I can move by clicking on screen (I have a coroutine to move character till x is the same) but i´m crazy about jumping.
I need to jump walking and stand.
How can i do this? I can share my code.
Thanks for the ping on twitter.
Wow, that’s a lot to cover in one forum post, but it does sound like a great project!
Pretty soon we’re coming out with a sidescroller prefab for iPhone on UnityPrefabs.com - it does a lot of things that will help you out.
For jumping though, we use buttons, not swipes, but you can easily code that in.
We’re using rigidbodies for characters to give it a smooth feel.
When starting out with Unity though, I’d recommend CharacterControllers instead.
-TT
I get it working using rigidbodies. To jump i use AddForce. With this i can control how far the character jumps and it runs well on iphone, always at 29 fps.
I control the jump states with a coroutine started when character jumps.
I have a cache variable where i store the character position on y axis and compare with actual position. When actual position is lower than cache, i take MaxPoint and finish coroutine.
With that i´m able to control animation during jump.
Oh, i have a “isGrounded” using a raycast OnCollisionEnter, i tagged the floor as Floor so i can know when character is grounded and when it´s colliding with another thing.
Maybe this isn´t the best way to do this but it runs well on iphone and works as i want.
Hope Tornado understand me and all the people who need something similar too. 
If someone have any question, just ask.
And thanks!
Sounds great!!
Perfect execution!
Oh, and good luck in the world-cup… hehe!
-TT
Thanks!!
Know i thinking how to do animation when walking left, jumping left. Maybe another animation set with reverse textures?
Ok, now i have a real problem. My AddForce calls are loosing Force. How could this happen? Values are the same, but after a seconds jumping suddendly character jump is smaller.
Maybe Rigydbody weight changes? How? Unity bug?
Did you add any other scripts or changes before this happened?
I find the problem.Character jumps but it doesn’t stop walking, it causes physics problems and animation ones too. Now it works perfect. Thanks!
Ok, no, i don´t find the problem. I´m really crazy with this. The problem is jumping:
- Character jumps some times
- On a jump, character animation revert to default texture and then it crash
- After this jump addforce stop working on any jump but animations work well again.
Here´s the code to jump (jump right but code is the same, just a small change). I´ve translated names to english so anyone can understand it:
void SaltarD(float Altura, float Distancia) //Height and Distance
{
saltando=true; //jumping
Sprite.PlayAnim(5); //First jump frame
StartCoroutine("ControlPuntoMax"); //Coroutine to handle max height point
if(Altura>140f) //Height
{
Altura=140f; //Height
}
if(Distancia>50f) //Distance
{
Distancia=50f; //Distance
}
thisrigidbody.AddForce(Vector3.up*Altura*5f);
thisrigidbody.AddForce(Vector3.right*Distancia*5f);
}
And this is the code for the coroutine:
IEnumerator ControlPuntoMax ()
{
Sprite.PlayAnim(6); //Second anim on jump (just a frame)
float Cachey=-1000f; //Variable to save last height
bool PuntoMax=false; //When we reach max point it turns true
while(saltando !PuntoMax) //while jumping and max point no reached
{
if(thistransform.position.y<=Cachey)
{
Sprite.PlayAnim(7); //3 animation(frame) on jump
PuntoMax=true;
}
Cachey=thistransform.position.y;
yield return new WaitForSeconds(0.01f);
}
yield return new WaitForSeconds(0.01f);
}
Hope someone can help me. Please!
I try it on Unity Iphone Advanced 1.5 and 1.7 with same result. Could it be a unity bug? I try to stop coroutines every time i start a new one to be sure there ins´t two coroutines at the same time but i get the same bug.
After some jumps forces crash. Addforce values are the same (i check it with a debug) so the problem is with Addforce.
I’m really not sure what causes this… could someone with more coding skills than me look at this??
-TT