0
\$\begingroup\$

My character is a square which can spin in any angle but it can not jump in certain angle.

Can anyone help?

public class Player: MonoBehaviour {

  public float maxspeed = 10f;
  bool facingRight = true;

  Animator anim;

  bool grounded = false;
  public Transform groundCheck;
  float groundRadius = 0.2f;
  public LayerMask whatIsGround;
  public float jumpFocre = 500f;

  bool doubleJump = false;

  void Start ()
  {
    anim = GetComponent<Animator> ();
  }

  void FixedUpdate () {
    grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    anim.SetBool ("Ground", grounded);

    if (grounded)
      doubleJump = false;

    float move = Input.GetAxis ("Horizontal");
    anim.SetFloat ("Speed", Mathf.Abs (move));

    GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxspeed, GetComponent<Rigidbody2D>().velocity.y);
  }

  void Update()
  {
    if((grounded || !doubleJump) && Input.GetKeyDown ("up"))
    {
      anim.SetBool("Ground", false);
      GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpFocre));

      if(!doubleJump && !grounded)
        doubleJump = true;
    }
  }

  void Flip()
  {
    facingRight = !facingRight;
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;    
  }
}
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Your code currently contains :

public float jumpFocre = 500f;
//...
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpFocre));

Rigidbody2D.AddForce() takes a Vector2 as a parameter, so this can be changed to:

public Vector2 jumpDirection = new Vector2(0f, 1f);
public float jumpFocre = 500f;
//...
GetComponent<Rigidbody2D>().AddForce(jumpDirection.normalized * jumpFocre);

This will allow you to control both the magnitude and direction of the force applied for your jump by changing the values of jumpFocre and jumpDirection.

\$\endgroup\$
7
  • \$\begingroup\$ I still can't jump. \$\endgroup\$
    – Bob
    Apr 28, 2015 at 10:10
  • \$\begingroup\$ jump direction should be modified otherwise your character will continue jump only on the y component. \$\endgroup\$
    – LaboPie
    Apr 28, 2015 at 10:31
  • \$\begingroup\$ How do I modified it? \$\endgroup\$
    – Bob
    Apr 28, 2015 at 11:28
  • \$\begingroup\$ (1,0) is right, (0,1) is up, (1,1) is halfway in between. \$\endgroup\$ Apr 28, 2015 at 11:32
  • \$\begingroup\$ How do I add them together? \$\endgroup\$
    – Bob
    Apr 28, 2015 at 12:07
0
\$\begingroup\$

Try something like this

public Vector2 jumpDirection = new Vector2(0f, 1f);
public float jumpFocre = 500f;
//...
float move = Input.GetAxis ("Horizontal");
jumpDirection.x = move;
GetComponent<Rigidbody2D>().AddForce(jumpDirection.normalized * jumpFocre);

In this way you are giving to the jump force the x component that the user are triggering in that moment.

\$\endgroup\$
7
  • \$\begingroup\$ Do I put this in void update or void fixedupdate? \$\endgroup\$
    – Bob
    Apr 28, 2015 at 14:00
  • \$\begingroup\$ The variable declaration outside the function. The rest of the snippet in the if statement that you have in your update function. \$\endgroup\$
    – LaboPie
    Apr 28, 2015 at 14:05
  • \$\begingroup\$ I still can't jump. \$\endgroup\$
    – Bob
    Apr 28, 2015 at 14:32
  • \$\begingroup\$ did you step into code, your code enter in the if statement in the update function? \$\endgroup\$
    – LaboPie
    Apr 28, 2015 at 14:50
  • \$\begingroup\$ If you put a breakpoint, it's triggered? \$\endgroup\$
    – LaboPie
    Apr 28, 2015 at 15:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .