- Home /
Problems with rigidbody2D.AddForce
Hi! , Im trying to get jumps like the game "the impossible game": https://www.youtube.com/watch?v=vW8nXTzroos
Well, I wrote the essential, some rigidbody2D.AddForce to make the jump etc, the PROBLEM is that when I click ( It add the force in Y) it add more force to X, and obviously I don't want that, I want to keep the velocity in the X.
And sorry for my english ^^ Ohh and if someone knows how to do these types of jumps like the video I posted, tell me please! I tried to change the gravity but I still without get this type of jumps hehe.
Here is the code, hope you can help me guys!:
using UnityEngine;
using System.Collections;
public class controladorPersonaje : MonoBehaviour {
public float fuerzaSalto = 100f;
public float fuerzaCorrer = 100f;
public bool enSuelo = true;
public bool enSuelo1 = true;
public bool enSuelo2 = true;
public bool enSuelo3 = true;
public bool enSuelo4 = true;
public Transform comprobadorSuelo;
float comprobadorRadio = 0.07f;
public Transform comprobadorSuelo1;
float comprobadorRadio1 = 0.07f;
public Transform comprobadorSuelo2;
float comprobadorRadio2 = 0.07f;
public Transform comprobadorSuelo3;
float comprobadorRadio3= 0.07f;
public Transform comprobadorSuelo4;
float comprobadorRadio4 = 0.07f;
public LayerMask mascaraSuelo;
private Animator animator;
void Awake(){
animator = GetComponent<Animator> ();
}
// Use this for initialization
void Start () {
}
void FixedUpdate(){
enSuelo = Physics2D.OverlapCircle (comprobadorSuelo.position, comprobadorRadio, mascaraSuelo);
animator.SetBool ("isGrounded", enSuelo);
enSuelo1 = Physics2D.OverlapCircle (comprobadorSuelo1.position, comprobadorRadio1, mascaraSuelo);
animator.SetBool ("isGrounded", enSuelo1);
enSuelo2 = Physics2D.OverlapCircle (comprobadorSuelo2.position, comprobadorRadio2, mascaraSuelo);
animator.SetBool ("isGrounded", enSuelo2);
enSuelo3 = Physics2D.OverlapCircle (comprobadorSuelo3.position, comprobadorRadio3, mascaraSuelo);
animator.SetBool ("isGrounded", enSuelo3);
enSuelo4 = Physics2D.OverlapCircle (comprobadorSuelo4.position, comprobadorRadio4, mascaraSuelo);
animator.SetBool ("isGrounded", enSuelo4);
rigidbody2D.AddForce (new Vector2 (fuerzaCorrer, 0));
if (enSuelo && Input.GetMouseButton (0) || enSuelo1 && Input.GetMouseButton (0)|| enSuelo2 && Input.GetMouseButton (0)|| enSuelo3 && Input.GetMouseButton (0)|| enSuelo4 && Input.GetMouseButton (0)) {
rigidbody2D.AddForce (new Vector2 (0, fuerzaSalto));
//transform.Rotate(new Vector3(0,0,70));
//transform.Rotate(0, Time.deltaTime * 30, 0, Space.Self);
}
}
// Update is called once per frame
void Update ()
{
}
}
I don't see the problem. You're adding fuerzaCorrer force on X each frame, and fuerzaSalto force on Y when you jump. What exactly do you want to do differently?
Yes, but when I jump it add more force on X, I mean the square goes more faster on X than if I dont jump
Answer by AlwaysSunny · Nov 02, 2014 at 08:16 PM
Oh, I get it. You're probably experiencing drag due to friction when your object is grounded. There are various ways to address this problem, and selecting the appropriate method depends on your preference for how the object should behave.
Since you want to move with the same X velocity at all times, the easiest way is probably giving the object a physics material with zero friction. Also, to maintain this same-X-velocity rule, you might consider setting the rigidbody's "drag" property to zero as well.
$$anonymous$$en, I don't know what to say, thanks isn't enough, seriously you made me happy this day, THAN$$anonymous$$S for all seriously and with that I got the jumps that I wanted like in the game :) Thanks!!!!
Sorry for my english :P
Your answer
Follow this Question
Related Questions
Rigidbody2D AddForce only working with large numbers 2 Answers
Jump problem 1 Answer
Unity 2D Physics .AddForce 1 Answer
Can someone help me with this Rigidbody2D bug? 1 Answer
Character instantly jumps instead of smoothly gaining altitude 3 Answers